Sunday, August 16, 2015

Leetcode 58. Length of Last Word

https://leetcode.com/problems/length-of-last-word/

Solution:
# T:O(n) S:O(1)
class Solution:
    # @param {string} s
    # @return {integer}
    def lengthOfLastWord(self, s):
        s = s.rstrip(' ')
        L, count = len(s), 0
        if L == 0: return 0
        for i in reversed(xrange(L)):
            if s[i] == ' ':
                break
            else:
                count += 1
        return count
Run Time: 48 ms

No comments:

Post a Comment