-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm1980 v1 trie.py
36 lines (30 loc) · 1.01 KB
/
m1980 v1 trie.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
class Solution:
def findDifferentBinaryString(self, nums: List[str]) -> str:
trie = {}
for num in nums :
curr = trie
for c in num :
curr[-1] = curr.get(-1, 0) + 1 # Count cases
if c not in curr :
curr[c] = {}
curr = curr[c]
output = []
while len(output) < len(nums[0]) :
if not trie :
output.append('0')
elif len(trie) == 3 :
zero = trie['0'].get(-1, 0)
one = trie['1'].get(-1, 0)
if zero > one :
output.append('0')
trie = trie.get('0')
else :
output.append('1')
trie = trie.get('1')
elif '0' in trie :
output.append('1')
trie = None
else :
output.append('0')
trie = None
return ''.join(output)