58. Length of Last Word

Updated: 2024-04-02
1 min read
[]

On This Page

LeetCode problem 58

class Solution:
    def lengthOfLastWord(self, s: str) -> int:
        i = len(s) - 1
        while i >= 0 and s[i] == ' ':
            i -= 1
        j = i
        while j >= 0 and s[j] != ' ':
            j -= 1
        return i - j
class Solution:
    def lengthOfLastWord(self, s: str) -> int:
        ar = s.split()
        return len(ar[-1])