Skip to content

Commit 1496744

Browse files
authored
Create Solution.java (Tahanima#92)
1 parent 5ca50f1 commit 1496744

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int lengthOfLastWord(String s) {
3+
int len = 0;
4+
boolean hasWordCountStarted = false;
5+
6+
for (int i = s.length() - 1; i > -1; i--) {
7+
if (s.charAt(i) == ' ' && hasWordCountStarted)
8+
return len;
9+
10+
if (s.charAt(i) != ' ') {
11+
hasWordCountStarted = true;
12+
len++;
13+
}
14+
}
15+
16+
return len;
17+
}
18+
}

0 commit comments

Comments
 (0)