Given a string, find the first non-repeating character in it and return its index. If it doesn’t exist, return -1.
Examples:
s = "leetcode"
return 0.
s = "loveleetcode"
return 2.
Note: You may assume the string contains only lowercase English letters.
Solution in python:
class Solution:
def firstUniqChar(self, s: str) -> int:
adic = {}
for i in range(len(s)):
if s[i] not in adic.keys():
adic[s[i]] = 1
else:
adic[s[i]] += 1
result = []
for key, value in adic.items():
if value == 1:
result.append(s.index(key))
if len(result):
return min(result)
else:
return -1
留言