Skip to content

Commit 6340e81

Browse files
committed
commit
1 parent ed13755 commit 6340e81

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

58. Length of Last Word.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int lengthOfLastWord(String s) {
3+
int counter = 0;
4+
5+
for (int idx = s.length() - 1; idx >= 0; idx--) {
6+
char curr_ch = s.charAt(idx);
7+
8+
// Check if current char is whitespace
9+
if (curr_ch == ' ') {
10+
// Check again if counter is 0, means whitespace at behind the last substring
11+
if (counter == 0)
12+
continue;
13+
14+
break;
15+
}
16+
17+
counter++;
18+
}
19+
20+
return counter;
21+
22+
/* Using split
23+
* String[] split_s = s.split(" ");
24+
*
25+
* return split_s[split_s.length - 1].length();
26+
*/
27+
}
28+
}

0 commit comments

Comments
 (0)