58. Length of Last Word

Обновлено: 2024-04-02
1 мин
[]

Содержание

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])