diff options
Diffstat (limited to 'first_unique_char_string.py')
-rw-r--r-- | first_unique_char_string.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/first_unique_char_string.py b/first_unique_char_string.py new file mode 100644 index 0000000..6909b5a --- /dev/null +++ b/first_unique_char_string.py @@ -0,0 +1,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")) |