Python Use Dictionary O(n)


  • 0
    Y

    dictionary store at most 26 letters, then one pass run of s.

        if not s:
            return -1
        
        dict = {}
    
        for i, letter in enumerate(s):
            if letter not in dict:
                dict[letter] = i
            else:
                dict[letter] = len(s)
        res = min(dict.values())
        return res if res != len(s) else -1

Log in to reply
 

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