-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC_1307.py
59 lines (48 loc) · 1.65 KB
/
LC_1307.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
56
57
58
59
from collections import Counter, defaultdict
from itertools import permutations
from typing import List
class Solution:
def isSolvable(self, words: List[str], result: str) -> bool:
c = Counter()
non_leading_zero = set()
for word in words:
if len(word) > 1:
non_leading_zero.add(word[0])
for i in range(len(word)):
c[word[i]] += 10 ** (len(word) - i - 1)
if len(result) > 1:
non_leading_zero.add(result[0])
for i in range(len(result)):
c[result[i]] -= 10 ** (len(result) - i - 1)
pos = []
neg = []
for l, v in c.items():
if v > 0:
pos.append((l, v))
elif v < 0:
neg.append((l, -v))
if len(pos) > len(neg):
neg, pos = pos, neg
sum_map = defaultdict(list)
for permu in permutations(list(range(10)), len(pos)):
if 0 in permu and pos[permu.index(0)][0] in non_leading_zero:
continue
s = 0
idx = 0
for (l, v) in pos:
s += v * permu[idx]
idx += 1
sum_map[s].append(set(permu))
for permu in permutations(list(range(10)), len(neg)):
if 0 in permu and neg[permu.index(0)][0] in non_leading_zero:
continue
s = 0
idx = 0
for (l, v) in neg:
s += v * permu[idx]
idx += 1
if s in sum_map:
for s2 in sum_map[s]:
if not set(permu) & s2:
return True
return False