434. Number of Segments in a String

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public int countSegments(String s) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != ' ') {
count++;
while (i < s.length() && s.charAt(i) != ' ') {
i++;
}
}
}

return count;
}
}

References

434. Number of Segments in a String