aboutsummaryrefslogtreecommitdiff
path: root/first_unique_char_string.py
blob: 6909b5a677caaa61aa0eb99b8c4b2aad12d69454 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 387. First Unique Character in a String

"""
given a string 's', find the first non-repeating character in it and return
its index. if it does not exist, return -1
"""


class Solution(object):
    def firstUniqChar(self, s):
        """
        :type s: str
        :rtype: int
        """
        m = {}
        for c in s:
            m[c] = m.get(c, 0) + 1
        for i in range(len(s)):
            if m[s[i]] == 1:
                return i
        return -1


if __name__ == "__main__":
    obj = Solution()
    print(obj.firstUniqChar("leetcode"))
    print(obj.firstUniqChar("loveleetcode"))
    print(obj.firstUniqChar("aabb"))