Number of Segments in a String 2021-10-07 21:02
public int countSegments(String s) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
if ((i == 0 || s.charAt(i - 1) == ' ') && s.charAt(i) != ' ') {
count++;
}
}
return count;
}
Runtime | Memory |
---|---|
0 ms | 37 MB |
EOF