Skip to content
LC-0387 Easy LeetCode

387. First Unique Character in a String

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 64% Topics: Hash Table, String, Queue, Counting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(n)

from collections import defaultdict

class Solution(object):
    def firstUniqChar(self, s):
        """
        :type s: str
        :rtype: int
        """
        lookup = defaultdict(int)
        candidtates = set()
        for i, c in enumerate(s):
            if lookup[c]:
                candidtates.discard(lookup[c])
            else:
                lookup[c] = i+1
                candidtates.add(i+1)

        return min(candidtates)-1 if candidtates else -1

Solution from kamyu104/LeetCode-Solutions · MIT