387. First Unique Character in a String
π© Easy
Question
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.
Code
from collections import Counter
def firstUniqChar(self, s: str) -> int:
for k, v in Counter(s).items():
# items() return the list with all dictionary keys with values
if v == 1:
return s.index(k)
return -1Python Counter Object
Last updated
Was this helpful?