There was an error while loading. Please reload this page.
1 parent 352e5d9 commit 85651ddCopy full SHA for 85651dd
alternative/easy/length_of_last_word.py
@@ -0,0 +1,19 @@
1
+def length_of_last_word(s):
2
+ count = 0
3
+ last_word = False
4
+ for c in reversed(s):
5
+ if c == ' ':
6
+ if last_word:
7
+ break
8
+ else:
9
+ last_word = True
10
+ count += 1
11
+ return count
12
+
13
+# Test cases
14
+assert length_of_last_word("Hello World") == 5
15
+assert length_of_last_word(" ") == 0
16
+assert length_of_last_word("a ") == 1
17
+assert length_of_last_word("a") == 1
18
+assert length_of_last_word("") == 0
19
+assert length_of_last_word(" fly me to the moon ") == 4
0 commit comments