-
Notifications
You must be signed in to change notification settings - Fork 0
/
387_first_unique_char_in_string.py
51 lines (40 loc) · 1.12 KB
/
387_first_unique_char_in_string.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'''
387. First Unique Character in a String
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.
'''
class Solution:
def firstUniqChar(self, s):
# dictionary1
dict = {}
for i in s:
dict[i] = dict.get(i, 0)+1
for i in dict:
if dict[i] == 1:
return s.index(i)
return -1
# dictionary2
# from collections import Counter
# count = collections.Counter(s)
# for i, letter in enumerate(s):
# if count[letter] == 1:
# return i
# return -1
# string slicing
# if not s:
# return -1
# if len(s) == 1:
# return 0
# for i, letter in enunmerate(s):
# if letter not in s[:i] + s[i+1:]:
# return i
# return -1
if __name__ == '__main__':
# begin
s = Solution()
print(s.firstUniqChar("loveleetcode"))