First Unique Character in a String

Problem Id: 387 Difficulty: Easy


Intuition

Solution


class Solution:
    def firstUniqChar(self, s: str) -> int:
        counts = {}
        for c in set(s):
            counts[c] = s.count(c)
        possibles = [c for c in counts.keys() if counts[c] == 1]
        if not possibles:
            return -1
        return min([s.index(c) for c in possibles])