LeetCode/387. First Unique Character in a String
https://leetcode.com/problems/first-unique-character-in-a-string
Basically this question is asking you about how to you Dictionary
. Understanding how to handle key
and value
and index
, can solve this problem easily.
My Solution
class Solution(object):
def firstUniqChar(self, s):
strDict = {}
for item in s:
if (item in strDict):
strDict[item] += 1
else:
strDict[item] = 1
for idx, key in enumerate(s):
if strDict[key] == 1:
return idx
return -1