给定一个字符串,找到其中第一个不重复的字符并返回其索引。
# 示例
# 输入:s = "leetcode"
# 输出:0
def firstUniqChar(s: str) -> int:
count = {}
for char in s:
if char in count:
count[char] += 1
else:
count[char] = 1
for i in range(len(s)):
if count[s[i]] == 1:
return i
return -1
# 调用函数
s = "leetcode"
result = firstUniqChar(s)
print(result) # 输出:0