""" 0058.1 - Length of Last Word - Solution 1 - Using strip() and split() """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def lengthOfLastWord(self, s: str) -> int:
"""Length of Last Word Function"""
return len(s.strip().split(" ")[-1])
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().lengthOfLastWord("Hello World")) # 5
print(Solution().lengthOfLastWord(" fly me to the moon ")) # 4
print(Solution().lengthOfLastWord("luffy is still joyboy")) # 6
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0058.2 - Length of Last Word - Solution 2 - Reverse Iteration with Pointer """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def lengthOfLastWord(self, s: str) -> int:
"""Length of Last Word Function"""
i = len(s) - 1
length = 0
# Skip trailing spaces
while i >= 0 and s[i] == ' ':
i -= 1
# Count characters of the last word
while i >= 0 and s[i] != ' ':
length += 1
i -= 1
return length
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().lengthOfLastWord("Hello World")) # 5
print(Solution().lengthOfLastWord(" fly me to the moon ")) # 4
print(Solution().lengthOfLastWord("luffy is still joyboy")) # 6
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()