diff --git a/lengthoflastword.py b/lengthoflastword.py new file mode 100644 index 0000000..ffb63f3 --- /dev/null +++ b/lengthoflastword.py @@ -0,0 +1,7 @@ +class Solution: + def lengthOfLastWord(self, s): + end = len(s) - 1 + while end > 0 and s[end] == " ": end -= 1 + beg = end + while beg >= 0 and s[beg] != " ": beg -= 1 + return end - beg