My wrong answer in Python got accepted! Incomplete test cases?


  • 0
    Q

    There are 100 different chars:

    >>> import string
    >>> string.printable
    '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:
    ;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
    >>> len(string.printable)
    100 
    

    So I thought 100 is an acceptable threshold to stopping the process. I mean, when the longest substring with 100 chars was found, stop and return. In my code, it was accepted with a threshhold 17!!! and 16 will be reported as a wrong answer.

    if len_longest_substring >= 17:
        return len_longest_substring
    

    Blow is my full accepted answer:

    class Solution:
        # @return an integer
        def lengthOfLongestSubstring(self, s):
            if len(s) <= 1 :
                return len(s)  
            longest_substring = s[0]
            len_longest_substring = 1
            s = s[1:] 
            index = 0
            while index < len(s):
                current_char = s[index] 
                if current_char not in longest_substring:
                    longest_substring = longest_substring + s[index]
                    index = index + 1
                else:
                    len_longest_substring = max(len_longest_substring, len(longest_substring))
                    if len_longest_substring >= 17:
                        return len_longest_substring
                    s = s[longest_substring.index(current_char):]
                    if s:
                        longest_substring = s[0]
                        s = s[1:]
                        index = 0
            len_longest_substring = max(len_longest_substring, len(longest_substring))
            return len_longest_substring

Log in to reply
 

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