Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
Examples:
s = "leetcode" return 0. s = "loveleetcode", return 2.
Note: You may assume the string contain only lowercase letters.
Subscribe to see which companies asked this question
并不难,用一个字典,记录每个字母出现次数。
然后再遍历一遍,如果是1次,那就是结果
class Solution(object):
def firstUniqChar(self, s):
dic = {}
for i in s:
dic[i] = dic.get(i,0) + 1
for i in s:
if dic[i] == 1:
return s.index(i)
return -1
"""
:type s: str
:rtype: int
"""