Skip to content

Commit fb16bcd

Browse files
authored
Create lengthOfLastWord.java
1 parent d167088 commit fb16bcd

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

lengthOfLastWord.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int lengthOfLastWord(String s) {
3+
if(s.length()==0){
4+
return 0;
5+
}
6+
int count = 0;
7+
int length = s.length()-1;
8+
int i = s.length()-1;
9+
while(s.charAt(i)==' '){
10+
length--;
11+
i--;
12+
if(i<0)
13+
return 0;
14+
}
15+
for(int j=length;j>=0;j--){
16+
if(s.charAt(j) ==' ')
17+
break;
18+
else
19+
count++;
20+
}
21+
return count;
22+
}
23+
}

0 commit comments

Comments
 (0)