title | seoTitle | description | toc | tags | categories | date | lastMod | featuredImage | weight | ||
---|---|---|---|---|---|---|---|---|---|---|---|
58. Length of Last Word |
LeetCode 58. Length of Last Word | Python solution and explanation |
58. Length of Last Word |
true |
|
2024-01-01 |
2024-01-01 |
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])