PROBLEM DESCRIPTION
Given a string s consisting of words and spaces, return the length of the last word in the string.
A word is a maximal substring consisting of non-space characters only.
SOLUTION
TWO LOOPS
This is an easy question specially if you use inbuilt function to trim/split the string. It can also be solved using two loops: - First loop to skip the training spaces - Second loop to calculate the length (keep iterating from back until we get an empty character)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public int lengthOfLastWord(String s) {
int length = 0;
int idx = s.length()-1;
//skip trailing spaces
while(true){
if(s.charAt(idx) == ' '){
idx--;
}else{
break;
}
}
while(idx>=0 && s.charAt(idx) != ' '){
length++;
idx--;
}
return length;
}
}
SINGLE LOOP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
public int lengthOfLastWord(String s) {
int length = 0;
//iterate from right to left
int idx = s.length() - 1;
while(idx >= 0){
//if character is not a blank
if(s.charAt(idx) != ' '){
//increase the length
length++;
//if it's a blank, check the value of length
//if length is 0, no need to do anything
//if length is more than 0, it must have been due to the last word, in which case we can return "length"
}else if(length > 0){
return length;
}
//go to next character on the left side
idx--;
}
return length;
}
}