58. Length of Last Word
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 56% Topics: String
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
class Solution(object):
# @param s, a string
# @return an integer
def lengthOfLastWord(self, s):
length = 0
for i in reversed(s):
if i == ' ':
if length:
break
else:
length += 1
return length
# Time: O(n)
# Space: O(n)
class Solution2(object):
# @param s, a string
# @return an integer
def lengthOfLastWord(self, s):
return len(s.strip().split(" ")[-1])
Solution from kamyu104/LeetCode-Solutions · MIT