Skip to content

Commit 7b3a50d

Browse files
Create Solution.py
1 parent d5f8fd4 commit 7b3a50d

File tree

1 file changed

+87
-0
lines changed
  • solution/1300-1399/1307.Verbal Arithmetic Puzzle

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
class Solution:
2+
def isAnyMapping(self, words, row, col, bal, letToDig, digToLet, totalRows, totalCols):
3+
# If traversed all columns.
4+
if col == totalCols:
5+
return bal == 0
6+
7+
# At the end of a particular column.
8+
if row == totalRows:
9+
return (bal % 10 == 0 and
10+
self.isAnyMapping(words, 0, col + 1, bal // 10, letToDig, digToLet, totalRows, totalCols))
11+
12+
w = words[row]
13+
14+
# If the current string 'w' has no character in the ('col')th index.
15+
if col >= len(w):
16+
return self.isAnyMapping(words, row + 1, col, bal, letToDig, digToLet, totalRows, totalCols)
17+
18+
# Take the current character in the variable letter.
19+
letter = w[len(w) - 1 - col]
20+
21+
# Create a variable 'sign' to check whether we have to add it or subtract it.
22+
if row < totalRows - 1:
23+
sign = 1
24+
else:
25+
sign = -1
26+
27+
# If we have a prior valid mapping, then use that mapping.
28+
# 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+
35+
# Choose a new mapping.
36+
else:
37+
for i in range(10):
38+
# 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):
40+
digToLet[i] = letter
41+
letToDig[letter] = i
42+
43+
# 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):
46+
return True
47+
48+
# Unselect the mapping.
49+
digToLet[i] = '-'
50+
if letter in letToDig:
51+
del letToDig[letter]
52+
53+
# If nothing is correct then just return false.
54+
return False
55+
56+
def isSolvable(self, words, result):
57+
# Add the string 'result' in the list 'words'.
58+
words.append(result)
59+
60+
# Initialize 'totalRows' with the size of the list.
61+
totalRows = len(words)
62+
63+
# Find the longest string in the list and set 'totalCols' with the size of that string.
64+
totalCols = max(len(word) for word in words)
65+
66+
# Create a HashMap for the letter to digit mapping.
67+
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)
73+
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
84+
85+
words3 = ["LEET", "CODE"]
86+
result3 = "POINT"
87+
print(sol.isSolvable(words3, result3)) # Output: False

0 commit comments

Comments
 (0)