thiagowfx's avatar

Β¬ just serendipity πŸ€ (not just serendipity)

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.