Skip to content

Commit dd85b2c

Browse files
committed
Added Solution for 1307. Verbal Arithmetic Puzzel and Updated README.md and README_EN.md
1 parent 11ee104 commit dd85b2c

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

solution/1300-1399/1307.Verbal Arithmetic Puzzle/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,45 @@
6969
<!-- 这里可写当前语言的特殊实现逻辑 -->
7070

7171
```python
72+
class Solution:
73+
def isSolvable(self, words: List[str], result: str) -> bool:
74+
words.append(result)
75+
rows = len(words)
76+
cols = max(map(len, words))
77+
letterToDigit = {}
78+
usedDigit = [False] * 10
79+
80+
def dfs(row: int, col: int, summ: int) -> bool:
81+
if col == cols:
82+
return summ == 0
83+
if row == rows:
84+
return summ % 10 == 0 and dfs(0, col + 1, summ // 10)
85+
86+
word = words[row]
87+
if col >= len(word):
88+
return dfs(row + 1, col, summ)
89+
90+
letter = word[~col]
91+
sign = -1 if row == rows - 1 else 1
92+
93+
if letter in letterToDigit and (
94+
letterToDigit[letter] > 0 or col < len(word) - 1
95+
):
96+
return dfs(row + 1, col, summ + sign * letterToDigit[letter])
97+
98+
for digit, used in enumerate(usedDigit):
99+
if not used and (digit > 0 or col < len(word) - 1):
100+
letterToDigit[letter] = digit
101+
usedDigit[digit] = True
102+
if dfs(row + 1, col, summ + sign * digit):
103+
return True
104+
usedDigit[digit] = False
105+
if letter in letterToDigit:
106+
del letterToDigit[letter]
107+
108+
return False
109+
110+
return dfs(0, 0, 0)
72111

73112
```
74113

solution/1300-1399/1307.Verbal Arithmetic Puzzle/README_EN.md

+39
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,45 @@ Note that two different characters cannot map to the same digit.
6060
### **Python3**
6161

6262
```python
63+
class Solution:
64+
def isSolvable(self, words: List[str], result: str) -> bool:
65+
words.append(result)
66+
rows = len(words)
67+
cols = max(map(len, words))
68+
letterToDigit = {}
69+
usedDigit = [False] * 10
70+
71+
def dfs(row: int, col: int, summ: int) -> bool:
72+
if col == cols:
73+
return summ == 0
74+
if row == rows:
75+
return summ % 10 == 0 and dfs(0, col + 1, summ // 10)
76+
77+
word = words[row]
78+
if col >= len(word):
79+
return dfs(row + 1, col, summ)
80+
81+
letter = word[~col]
82+
sign = -1 if row == rows - 1 else 1
83+
84+
if letter in letterToDigit and (
85+
letterToDigit[letter] > 0 or col < len(word) - 1
86+
):
87+
return dfs(row + 1, col, summ + sign * letterToDigit[letter])
88+
89+
for digit, used in enumerate(usedDigit):
90+
if not used and (digit > 0 or col < len(word) - 1):
91+
letterToDigit[letter] = digit
92+
usedDigit[digit] = True
93+
if dfs(row + 1, col, summ + sign * digit):
94+
return True
95+
usedDigit[digit] = False
96+
if letter in letterToDigit:
97+
del letterToDigit[letter]
98+
99+
return False
100+
101+
return dfs(0, 0, 0)
63102

64103
```
65104

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution:
2+
def isSolvable(self, words: List[str], result: str) -> bool:
3+
words.append(result)
4+
rows = len(words)
5+
cols = max(map(len, words))
6+
letterToDigit = {}
7+
usedDigit = [False] * 10
8+
9+
def dfs(row: int, col: int, summ: int) -> bool:
10+
if col == cols:
11+
return summ == 0
12+
if row == rows:
13+
return summ % 10 == 0 and dfs(0, col + 1, summ // 10)
14+
15+
word = words[row]
16+
if col >= len(word):
17+
return dfs(row + 1, col, summ)
18+
19+
letter = word[~col]
20+
sign = -1 if row == rows - 1 else 1
21+
22+
if letter in letterToDigit and (
23+
letterToDigit[letter] > 0 or col < len(word) - 1
24+
):
25+
return dfs(row + 1, col, summ + sign * letterToDigit[letter])
26+
27+
for digit, used in enumerate(usedDigit):
28+
if not used and (digit > 0 or col < len(word) - 1):
29+
letterToDigit[letter] = digit
30+
usedDigit[digit] = True
31+
if dfs(row + 1, col, summ + sign * digit):
32+
return True
33+
usedDigit[digit] = False
34+
if letter in letterToDigit:
35+
del letterToDigit[letter]
36+
37+
return False
38+
39+
return dfs(0, 0, 0)

0 commit comments

Comments
 (0)