-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathLENGTHLAST.java
71 lines (57 loc) · 1.62 KB
/
LENGTHLAST.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.
//
//If the last word does not exist, return 0.
//
//Note: A word is defined as a character sequence consists of non-space characters only.
//
//Example:
//
//Given s = "Hello World",
//
//return 5 as length("World") = 5.
//
//Please make sure you try to solve this problem without using library functions. Make sure you only traverse the string once.
public class Solution {
public int lengthOfLastWord(final String a) {
int count = 0;
boolean foundSpace = false;
if(a.length() == 1){
if(a.charAt(0) != ' '){
count++;
}
}else {
for(int i=a.length()-1; i >=0;i--){
if(a.charAt(i) != ' '){
count++;
}else if(a.charAt(i) == ' '&& count == 0){
foundSpace = true;
continue;
}else if(foundSpace || count >0){
break;
}
}
}
return count;
}
}
public class Solution {
public int lengthOfLastWord(final String A) {
int i;
if (A.length() == 0)
return 0;
int n = A.length();
int lastWordIndex = n;
int endIndex = n;
i = n - 1;
while (i >= 0 && A.charAt(i) == ' ')
i--;
endIndex = i;
for (; i >= 0; i--) {
if (A.charAt(i) == ' ') {
break;
}
lastWordIndex = i;
}
return Math.max(0, endIndex - lastWordIndex + 1);
}
}