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
最后修改日期: 2021年1月26日

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。