Skip to content

Commit f742700

Browse files
authored
Update Solution.py
1 parent 7b3a50d commit f742700

File tree

1 file changed

+45
-32
lines changed
  • solution/1300-1399/1307.Verbal Arithmetic Puzzle

1 file changed

+45
-32
lines changed
Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
class Solution:
2-
def isAnyMapping(self, words, row, col, bal, letToDig, digToLet, totalRows, totalCols):
2+
def isAnyMapping(
3+
self, words, row, col, bal, letToDig, digToLet, totalRows, totalCols
4+
):
35
# If traversed all columns.
46
if col == totalCols:
57
return bal == 0
68

79
# At the end of a particular column.
810
if row == totalRows:
9-
return (bal % 10 == 0 and
10-
self.isAnyMapping(words, 0, col + 1, bal // 10, letToDig, digToLet, totalRows, totalCols))
11+
return bal % 10 == 0 and self.isAnyMapping(
12+
words, 0, col + 1, bal // 10, letToDig, digToLet, totalRows, totalCols
13+
)
1114

1215
w = words[row]
1316

1417
# If the current string 'w' has no character in the ('col')th index.
1518
if col >= len(w):
16-
return self.isAnyMapping(words, row + 1, col, bal, letToDig, digToLet, totalRows, totalCols)
19+
return self.isAnyMapping(
20+
words, row + 1, col, bal, letToDig, digToLet, totalRows, totalCols
21+
)
1722

1823
# Take the current character in the variable letter.
1924
letter = w[len(w) - 1 - col]
@@ -26,27 +31,48 @@ def isAnyMapping(self, words, row, col, bal, letToDig, digToLet, totalRows, tota
2631

2732
# If we have a prior valid mapping, then use that mapping.
2833
# The second condition is for the leading zeros.
29-
if (letter in letToDig and
30-
(letToDig[letter] != 0 or (letToDig[letter] == 0 and len(w) == 1) or col != len(w) - 1)):
31-
32-
return self.isAnyMapping(words, row + 1, col, bal + sign * letToDig[letter],
33-
letToDig, digToLet, totalRows, totalCols)
34+
if letter in letToDig and (
35+
letToDig[letter] != 0
36+
or (letToDig[letter] == 0 and len(w) == 1)
37+
or col != len(w) - 1
38+
):
39+
40+
return self.isAnyMapping(
41+
words,
42+
row + 1,
43+
col,
44+
bal + sign * letToDig[letter],
45+
letToDig,
46+
digToLet,
47+
totalRows,
48+
totalCols,
49+
)
3450

3551
# Choose a new mapping.
3652
else:
3753
for i in range(10):
3854
# If 'i'th mapping is valid then select it.
39-
if digToLet[i] == '-' and (i != 0 or (i == 0 and len(w) == 1) or col != len(w) - 1):
55+
if digToLet[i] == "-" and (
56+
i != 0 or (i == 0 and len(w) == 1) or col != len(w) - 1
57+
):
4058
digToLet[i] = letter
4159
letToDig[letter] = i
42-
60+
4361
# Call the function again with the new mapping.
44-
if self.isAnyMapping(words, row + 1, col, bal + sign * letToDig[letter],
45-
letToDig, digToLet, totalRows, totalCols):
62+
if self.isAnyMapping(
63+
words,
64+
row + 1,
65+
col,
66+
bal + sign * letToDig[letter],
67+
letToDig,
68+
digToLet,
69+
totalRows,
70+
totalCols,
71+
):
4672
return True
4773

4874
# Unselect the mapping.
49-
digToLet[i] = '-'
75+
digToLet[i] = "-"
5076
if letter in letToDig:
5177
del letToDig[letter]
5278

@@ -65,23 +91,10 @@ def isSolvable(self, words, result):
6591

6692
# Create a HashMap for the letter to digit mapping.
6793
letToDig = {}
68-
69-
# Create a list for the digit to letter mapping.
70-
digToLet = ['-'] * 10
71-
72-
return self.isAnyMapping(words, 0, 0, 0, letToDig, digToLet, totalRows, totalCols)
7394

74-
# Example usage:
75-
sol = Solution()
76-
77-
words1 = ["SEND", "MORE"]
78-
result1 = "MONEY"
79-
print(sol.isSolvable(words1, result1)) # Output: True
80-
81-
words2 = ["SIX", "SEVEN", "SEVEN"]
82-
result2 = "TWENTY"
83-
print(sol.isSolvable(words2, result2)) # Output: True
95+
# Create a list for the digit to letter mapping.
96+
digToLet = ["-"] * 10
8497

85-
words3 = ["LEET", "CODE"]
86-
result3 = "POINT"
87-
print(sol.isSolvable(words3, result3)) # Output: False
98+
return self.isAnyMapping(
99+
words, 0, 0, 0, letToDig, digToLet, totalRows, totalCols
100+
)

0 commit comments

Comments
 (0)