forked from lilianweng/LeetcodePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlen_last_word.py
More file actions
26 lines (22 loc) · 827 Bytes
/
len_last_word.py
File metadata and controls
26 lines (22 loc) · 827 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python
'''
Leetcode: Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.
If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space characters only.
For example, Given s = "Hello World", return 5.
'''
from __future__ import division
import random
def last_word(s):
# upper/lower-case alphabets and empty space
# remove spaces at the end
while s and not s[-1].isalpha(): s = s[:-1]
length = 0
for i in reversed(xrange(len(s))):
if s[i].isalpha(): length += 1
else: return length
return length
if __name__ == '__main__':
print last_word("Hello World")
print last_word(" ")
print last_word(" a a a ")