-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathanswer.py
30 lines (28 loc) · 904 Bytes
/
answer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python
#-------------------------------------------------------------------------------
class Solution:
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
length = len(s)
count = 0
for i in range(length):
if s[i] == ' ':
# If there are consecutive spaces at the end!
space = True
while space:
if i+1 < length:
if s[i+1] == ' ':
i += 1
else:
count = 0
space = False
else:
space = False
else:
count += 1
return count
#-------------------------------------------------------------------------------
# Testing