문제
Given a string s consists of upper/lower-case alphabets and empty space characters ' '
, return the length of last word (last word means the last appearing word if we loop from left to right) in the string.
If the last word does not exist, return 0.
Note: A word is defined as a maximal substring consisting of non-space characters only.
Example:
Input: "Hello World"
Output: 5
풀이
class Solution:
def lengthOfLastWord(self, s: str) -> int:
cnt = 0
prv = 0
for ch in s :
if ch == ' ' :
if cnt != 0 :
prv = cnt
cnt = 0
else :
cnt += 1
if cnt == 0 :
return prv
else :
return cnt
'알고리즘 트레이닝 > LeetCode' 카테고리의 다른 글
70. Climbing Stairs (0) | 2020.05.08 |
---|---|
69. Sqrt(x) (0) | 2020.05.08 |
35. Search Insert Position (0) | 2020.05.07 |
28. Implement strStr() (0) | 2020.05.06 |
27. Remove Element (0) | 2020.05.06 |