Python Use Dictionary O(n)

  • 0

    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
                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.