Very fast python code 44ms


  • 0
    A

    class Solution:
    # @param {string} s
    # @return {integer}

    def lengthOfLastWord(self, s):
        counter = 0
        index = len(s) - 1
        
        while index >= 0 and s[index] == ' ':
            index -= 1    
            
        while index >= 0 and s[index] != ' ':
            counter += 1
            index -= 1
            
        return counter

  • 0

    I wouldn't call this "very fast". If you check the statistics on the submission details page, you can see that about one in three is this fast or even faster. The best is 36ms, the mode and mean are 48ms, the mean is 50.7ms.


  • 0
    A

    Well, I'm only twelve so it's fast for me. I just started programming.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.