-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxOccurrences.pyi
79 lines (48 loc) · 1.63 KB
/
MaxOccurrences.pyi
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
######max occurrence solutions:
# mystr = "aabbccsbd"
def moc(mystring):
"""
1. create a new list with unique char
2. Now count each char from new list in original list and see which has max count
:param mystring:
:return:
newlist = ['a', 'b', 'c']
countlist = [1,3,4]
K (max of countlist) = 4
option 1:
-> find idx on K in countlist (i.e. 2)
-> find value in newlist with same idx (i.e. 'c')
option 2:
-> zip newlist (as keys) and countlist (as values)
-> find the key where value is max_count
"""
mylist = list(mystring)
newlist = []
countlist = []
l = print(len(mystring))
# 1. create a new list with unique char
for x in mylist:
if x not in newlist:
newlist.append(x)
print("newlist = %s" %newlist)
# 2. Now count each char from new list in original list and see which has max count
for y in newlist:
# print('for char %s in string the count sn %s' %(y, mylist.count(y) ))
countlist.append(mylist.count(y))
print("countlist = %s"%countlist)
max_count = max(countlist)
print("max value K = %s" % max_count )
################### option 1
# get the char from newlist that matches k
idxofKinCountList = countlist.index(max_count)
print("idxofKinCountList = %s" %idxofKinCountList)
print(newlist[idxofKinCountList])
#################### option 2
zipped_dict = dict(zip(newlist, countlist))
print(zipped_dict)
for key,val in zipped_dict.items():
if val == max_count:
print(key)
if __name__ == '__main__':
s = 'aaaabbcc'
moc(s)