Wonder why my code isn't correct

  • 0
    class Solution:
    # @param {string} s
    # @return {integer}
    def lengthOfLastWord(self, s):
        count = 0
        if len(s) == 0:
            count = 0
            space_index = s.rfind(' ')
            if space_index == -1:
                count = len(s)
                count = len(s)-space_index -1
        return count

    The LeetCode said it didn't pass the test example "a". It's expected to be 1, but output is 0.

    However, I run it in my computer, it does give output of 1. Could anyone help me?


  • 0

    The string may like "Hello World ". The end of string are some space characters.
    So, count = len(s)-space_index -1 seems not correct.

Log in to reply

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