LeetCode #58: Length of Last Word
• 50 words • 1 min • updated
LeetCode #58: Length of Last Word:
python
class Solution:
def lengthOfLastWord(self, s: str) -> int:
return len(s.split()[-1]).split() acts on whitespace by default. It is equivalent to .split(' ').
Previously:
python
class Solution:
def lengthOfLastWord(self, s: str) -> int:
return len(s.strip().split(' ')[-1])It turns out .strip() is not really necessary. ∎
Connections
Shared tags