-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirstNonRepeatingCharacter.py
55 lines (39 loc) · 1.25 KB
/
firstNonRepeatingCharacter.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
52
53
54
55
class FirstNonRepeatingCharacter():
def __init__(self, aString):
self.aString = aString
self.size = len(aString)
"""
aaabccdeeef => b
abcbad => c
asdfghasdfgh => None
"""
def solutionA(self):
_dict = {}
_banned = set()
for index, char in enumerate(self.aString):
if (char not in _dict and char not in _banned):
_dict[char] = index
elif (char in _dict):
_dict.pop(char)
_banned.add(char)
if not _dict:
return None
return self.aString[min(_dict.values())]
def solutionB(self):
_dict = {}
for char in self.aString:
if (char not in _dict):
_dict[char] = 1
else:
_dict[char] += 1
"""
{a:3,b:1,c:2,d:1,e:3,f:1}
"""
for char in self.aString
if (_dict[char] == 1):
return char
return None
if __name__ == '__main__':
for word in ['aaabccdeeef', 'abcbad', 'asdfghasdfgh']:
obj = FirstNonRepeatingCharacter(word)
print(obj.solutionA() == obj.solutionB())