1. Topic description![](https://img-blog.csdnimg.cn/8e0281da82d948d6a965a4288c45000c.png)
You are given a string
s
consisting of several words separated by some space characters before and after them. Returns the length of the last word in the string.A word is the largest substring consisting of only letters and not containing any space characters.
Example 1:
Input: s = "Hello World" Output: 5 Explanation: The last word is "World" and has a length of 5.Example 2:
Input: s = "fly me to the moon" Output: 4 Explanation: The last word is "moon" and has a length of 4.Example 3:
Input: s = "luffy is still joyboy" Output: 6 Explanation: The last word is "joyboy" of length 6.hint:
1 <= s.length <= 104
s
' '
Consists of only English letters and spacess
at least one word exists in
2. Core code![](https://img-blog.csdnimg.cn/22e871863fb1470695a615e913a1ac71.png)
class Solution {
public int lengthOfLastWord(String s) {
String[] split = s.split(" ");
return split[split.length - 1].length();
}
}
3. Test code![](https://img-blog.csdnimg.cn/7ab6202bb36d45b482b1efac81da9a4c.png)
class Solution {
public int lengthOfLastWord(String s) {
String[] split = s.split(" ");
return split[split.length - 1].length();
}
}
// @solution-sync:end
class Main {
public static void main(String[] args) {
String s = "Hello World";
int result = new Solution().lengthOfLastWord(s);
System.out.println(result);
}
}