forked from tcandzq/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstUniqueCharacterInAString.py
More file actions
50 lines (42 loc) · 1.2 KB
/
FirstUniqueCharacterInAString.py
File metadata and controls
50 lines (42 loc) · 1.2 KB
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
"""
题号 387 字符串中的第一个唯一字符
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
案例:
s = "leetcode"
返回 0.
s = "loveleetcode",
返回 2.
注意事项:您可以假定该字符串只包含小写字母。
"""
class Solution:
def firstUniqChar(self, s: str) -> int:
from _collections import OrderedDict
order_dict = OrderedDict()
for char in s:
order_dict[char] = order_dict.get(char,0)+1
for key,val in order_dict.items():
if val == 1:
return s.index(key)
return -1
# 使用collections.Counter()很巧妙
def firstUniqChar2(self, s):
"""
:type s: str
:rtype: int
"""
# build hash map : character and how often it appears
import collections
count = collections.Counter(s)
# find the index
index = 0
for ch in s:
if count[ch] == 1:
return index
else:
index += 1
return -1
if __name__ == '__main__':
s = "loveleetcode"
solution = Solution()
print(solution.firstUniqChar(s))