File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change 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 ])
You can’t perform that action at this time.
0 commit comments