Search

58. Length of Last Word

Given a string s consisting of words and spaces, return the length of the last word in the string.
word is a maximal substring consisting of non-space characters only.
Example 1:
Input: s = "Hello World" Output: 5 Explanation: The last word is "World" with length 5.
Plain Text
복사
Example 2:
Input: s = " fly me to the moon " Output: 4 Explanation: The last word is "moon" with length 4.
Plain Text
복사
Example 3:
Input: s = "luffy is still joyboy" Output: 6 Explanation: The last word is "joyboy" with length 6.
Plain Text
복사
Constraints:
1 <= s.length <= 104
s consists of only English letters and spaces ' '.
There will be at least one word in s.
link to the problem
Based on the problem constraints, the given string has a maximum length of 10410^4104 characters (including spaces) and consists only of English letters and spaces (' '). Also, the input string s contains at least one word.
My idea is to first iterate through the string s, collect only the non-space characters into a separate array, and then return the length of the word at the last index of that array. Since I’m solving this in Python, using the split() function would be more convenient, but I wanted to try solving it without using split().

First Try

class Solution: def lengthOfLastWord(self, s: str) -> int: lst = [] tmp = '' for v in s: if v != '' and v != ' ': tmp += v else: if tmp != '': lst.append(tmp) tmp = '' # To get rid of '' in lst #lst = [v for v in lst if v != ''] return len(lst[-1])
Python
복사
I wrote a function that iterates through the given string s, concatenates the characters excluding spaces to form words, stores them in a new array, and then returns the length of the last word.
The test cases passed without issues for case 1 and case 2, but it was judged incorrect on case 3.
I ran the corresponding test case locally and printed the result, but the array did not include the last word, joyboy.
When I printed tmp using the print() function, each word seemed to be generated correctly, but looking at the elements of the array printed to the console, joyboy was not included. I think the issue lies in the process of appending to lst.
Reviewing the code, I realized that the logic for appending to lst only occurs in the else block, which is executed when a space is encountered. Since there is no space after joyboy, the else block is never triggered.
To fix this, as shown in the code below, you should add one more check after the loop ends to append tmp to lst, so that the last word joyboy will also be included.

Second Try

class Solution: def lengthOfLastWord(self, s: str) -> int: lst = [] tmp = '' for v in s: if v != '' and v != ' ': tmp += v else: print(f' => {tmp}') if tmp != '': lst.append(tmp) tmp = '' if tmp != '': lst.append(tmp) # To get rid of '' in lst #lst = [v for v in lst if v != ''] return len(lst[-1])
Python
복사
I have modified the code, and now I will run the test cases again.
The test cases passed successfully. Now, I will go ahead and submit the solution.
I submitted the solution, and it passed. The issue with this code was that the logic only appended tmp to lst when a space was encountered at the end of the loop. In fact, it might be cleaner to simply include everything, including spaces, and then remove the spaces at the end.