Skip to content

Commit 2528221

Browse files
58. Length of Last Word
1 parent 94643f5 commit 2528221

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Easy/58.LengthofLastWord(list).py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Given a string s consists of upper/lower-case alphabets and empty space
3+
characters ' ', return the length of last word (last word means the last
4+
appearing word if we loop from left to right) in the string.
5+
If the last word does not exist, return 0.
6+
Note: A word is defined as a maximal substring consisting of non-space
7+
characters only.
8+
9+
Example:
10+
Input: "Hello World"
11+
Output: 5
12+
"""
13+
#Difficulty: Easy
14+
#59 / 59 test cases passed.
15+
#Runtime: 24 ms
16+
#Memory Usage: 14 MB
17+
18+
#Runtime: 24 ms, faster than 91.10% of Python3 online submissions for Length of Last Word.
19+
#Memory Usage: 14 MB, less than 37.01% of Python3 online submissions for Length of Last Word.
20+
21+
class Solution:
22+
def lengthOfLastWord(self, s: str) -> int:
23+
words = s.rstrip().split(" ")
24+
return len(words[-1])

0 commit comments

Comments
 (0)