Q: 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.
A:
class Solution(object):
def firstUniqChar(self, s):
"""
:type s: str
:rtype: int
"""
minIndex = len(s)
charList = [ch
for ch
in set(s)
if s.count(ch) ==
1]
if len(charList) ==
0:
return -
1
for ch
in charList:
if s.index(ch) < minIndex:
minIndex = s.index(ch)
return minIndex