class Solution: | |||||
def firstUniqChar(self, s): | |||||
seen = dict() | |||||
for x in range(len(s)): | |||||
if s[x] in seen: | |||||
seen[s[x]][0] += 1 | |||||
else: | |||||
seen[s[x]] = [1, x] | |||||
result = None | |||||
for key, val in seen.items(): | |||||
if val[0] == 1 and (result is None or val[1] < result): | |||||
result = val[1] | |||||
if result is None: | |||||
return -1 | |||||
return result | |||||
s = Solution() | |||||
print("Expected: 0") | |||||
print("Got:", s.firstUniqChar("leetcode")) |
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. |
#!/bin/bash | |||||
python3 main.py |