뒤에서부터 세기.
class Solution {
public int lengthOfLastWord(String s) {
int length = s.length();
boolean letterCameUpAlready = false;
int count = 0;
for (int i = length - 1; i >= 0; i--) {
if (letterCameUpAlready && s.charAt(i) == ' ') {
return count;
}
if (!letterCameUpAlready && s.charAt(i) != ' ') {
letterCameUpAlready = true;
count++;
} else {
if (letterCameUpAlready) {
count++;
}
}
}
return count;
}
}