-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverbalArithmeticPuzzle.py
More file actions
69 lines (62 loc) · 2.36 KB
/
verbalArithmeticPuzzle.py
File metadata and controls
69 lines (62 loc) · 2.36 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/verbal-arithmetic-puzzle/
# Author: Miao Zhang
# Date: 2021-04-22
class Solution:
def isSolvable(self, words: List[str], result: str) -> bool:
chars = [-1 for _ in range(128)]
visited = [0 for _ in range(10)]
result = result[::-1]
for word in words:
if len(word) > len(result): return False
words = [word[::-1] for word in words]
def dfs(i: int, j: int, sums: int) -> bool:
if j == len(result):
if sums != 0:
return False
if len(result) > 1 and chars[ord(result[-1])] == 0:
return False
return True
if i == len(words):
ch = ord(result[j])
if chars[ch] != -1:
if chars[ch] != sums % 10:
return False
return dfs(0, j + 1, sums // 10)
else:
if visited[sums % 10] == 1:
return False
chars[ch] = sums % 10
visited[sums % 10] = 1
if dfs(0, j + 1, sums // 10):
return True
chars[ch] = -1
visited[sums % 10] = 0
return False
if j >= len(words[i]):
return dfs(i + 1, j, sums)
ch = ord(words[i][j])
if chars[ch] != -1:
if len(words[i]) > 1 and j == len(words[i]) - 1 and chars[ch] == 0:
return False
return dfs(i + 1, j, sums + chars[ch])
else:
for d in range(10):
if visited[d] == 1:
continue
if d == 0 and len(words[i]) > 1 and j == len(words[i]) - 1:
continue
chars[ch] = d
visited[d] = 1
if dfs(i + 1, j, sums + d):
return True
chars[ch] = -1
visited[d] = 0
return False
return True
res = dfs(0, 0, 0)
for i, c in enumerate(chars):
if c != -1:
print(chr(i), c)
return res