1. Description
Given a string s consisting of words and spaces, return the length of the last word in the string.
A word is a maximal substring consisting of non-space characters only.
constraints :
- 1 <= s.length <= 104
- s consists of only English letters and spaces ' '.
- There will be at least one word in s.
2. Algorithms
- Delete blanks in each side using str.strip() method, and split by ' ' using str.split() method.
- To get last value, make result as list using list() function and store them in s.
- Return the length of last word.
Space Complexity : O(A) (A stands for count of words in s)
3. Codes
class Solution:
def lengthOfLastWord(self, s: str) -> int:
s = list(s.strip().split(' '))
return len(s[-1])
4. Conclusion
'LeetCode > Easy' 카테고리의 다른 글
67 Add Binary (0) | 2022.07.21 |
---|---|
66 Plus One (0) | 2022.07.21 |
35 Search Insert Position (0) | 2022.07.20 |
28 Implement strStr() (0) | 2022.07.20 |
27 Remove Elements (0) | 2022.07.19 |