Skip to content

Commit 06d7f60

Browse files
feat: add solutions to lc problem: No.1307 (doocs#3391)
1 parent 83b9f0f commit 06d7f60

File tree

3 files changed

+695
-6
lines changed

3 files changed

+695
-6
lines changed

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

+296-3
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,312 @@ tags:
8686
#### Python3
8787

8888
```python
89-
89+
class Solution:
90+
def isAnyMapping(
91+
self, words, row, col, bal, letToDig, digToLet, totalRows, totalCols
92+
):
93+
# If traversed all columns.
94+
if col == totalCols:
95+
return bal == 0
96+
97+
# At the end of a particular column.
98+
if row == totalRows:
99+
return bal % 10 == 0 and self.isAnyMapping(
100+
words, 0, col + 1, bal // 10, letToDig, digToLet, totalRows, totalCols
101+
)
102+
103+
w = words[row]
104+
105+
# If the current string 'w' has no character in the ('col')th index.
106+
if col >= len(w):
107+
return self.isAnyMapping(
108+
words, row + 1, col, bal, letToDig, digToLet, totalRows, totalCols
109+
)
110+
111+
# Take the current character in the variable letter.
112+
letter = w[len(w) - 1 - col]
113+
114+
# Create a variable 'sign' to check whether we have to add it or subtract it.
115+
if row < totalRows - 1:
116+
sign = 1
117+
else:
118+
sign = -1
119+
120+
# If we have a prior valid mapping, then use that mapping.
121+
# The second condition is for the leading zeros.
122+
if letter in letToDig and (
123+
letToDig[letter] != 0
124+
or (letToDig[letter] == 0 and len(w) == 1)
125+
or col != len(w) - 1
126+
):
127+
128+
return self.isAnyMapping(
129+
words,
130+
row + 1,
131+
col,
132+
bal + sign * letToDig[letter],
133+
letToDig,
134+
digToLet,
135+
totalRows,
136+
totalCols,
137+
)
138+
139+
# Choose a new mapping.
140+
else:
141+
for i in range(10):
142+
# If 'i'th mapping is valid then select it.
143+
if digToLet[i] == "-" and (
144+
i != 0 or (i == 0 and len(w) == 1) or col != len(w) - 1
145+
):
146+
digToLet[i] = letter
147+
letToDig[letter] = i
148+
149+
# Call the function again with the new mapping.
150+
if self.isAnyMapping(
151+
words,
152+
row + 1,
153+
col,
154+
bal + sign * letToDig[letter],
155+
letToDig,
156+
digToLet,
157+
totalRows,
158+
totalCols,
159+
):
160+
return True
161+
162+
# Unselect the mapping.
163+
digToLet[i] = "-"
164+
if letter in letToDig:
165+
del letToDig[letter]
166+
167+
# If nothing is correct then just return false.
168+
return False
169+
170+
def isSolvable(self, words, result):
171+
# Add the string 'result' in the list 'words'.
172+
words.append(result)
173+
174+
# Initialize 'totalRows' with the size of the list.
175+
totalRows = len(words)
176+
177+
# Find the longest string in the list and set 'totalCols' with the size of that string.
178+
totalCols = max(len(word) for word in words)
179+
180+
# Create a HashMap for the letter to digit mapping.
181+
letToDig = {}
182+
183+
# Create a list for the digit to letter mapping.
184+
digToLet = ["-"] * 10
185+
186+
return self.isAnyMapping(
187+
words, 0, 0, 0, letToDig, digToLet, totalRows, totalCols
188+
)
90189
```
91190

92191
#### Java
93192

94193
```java
95-
194+
class Solution {
195+
private boolean isAnyMapping(List<String> words, int row, int col, int bal,
196+
HashMap<Character, Integer> letToDig, char[] digToLet, int totalRows, int totalCols) {
197+
// If traversed all columns.
198+
if (col == totalCols) {
199+
return bal == 0;
200+
}
201+
202+
// At the end of a particular column.
203+
if (row == totalRows) {
204+
return (bal % 10 == 0
205+
&& isAnyMapping(
206+
words, 0, col + 1, bal / 10, letToDig, digToLet, totalRows, totalCols));
207+
}
208+
209+
String w = words.get(row);
210+
211+
// If the current string 'w' has no character in the ('col')th index.
212+
if (col >= w.length()) {
213+
return isAnyMapping(words, row + 1, col, bal, letToDig, digToLet, totalRows, totalCols);
214+
}
215+
216+
// Take the current character in the variable letter.
217+
char letter = w.charAt(w.length() - 1 - col);
218+
219+
// Create a variable 'sign' to check whether we have to add it or subtract it.
220+
int sign = (row < totalRows - 1) ? 1 : -1;
221+
222+
// If we have a prior valid mapping, then use that mapping.
223+
// The second condition is for the leading zeros.
224+
if (letToDig.containsKey(letter)
225+
&& (letToDig.get(letter) != 0 || (letToDig.get(letter) == 0 && w.length() == 1)
226+
|| col != w.length() - 1)) {
227+
228+
return isAnyMapping(words, row + 1, col, bal + sign * letToDig.get(letter), letToDig,
229+
digToLet, totalRows, totalCols);
230+
231+
} else {
232+
// Choose a new mapping.
233+
for (int i = 0; i < 10; i++) {
234+
// If 'i'th mapping is valid then select it.
235+
if (digToLet[i] == '-'
236+
&& (i != 0 || (i == 0 && w.length() == 1) || col != w.length() - 1)) {
237+
digToLet[i] = letter;
238+
letToDig.put(letter, i);
239+
240+
// Call the function again with the new mapping.
241+
if (isAnyMapping(words, row + 1, col, bal + sign * letToDig.get(letter),
242+
letToDig, digToLet, totalRows, totalCols)) {
243+
return true;
244+
}
245+
246+
// Unselect the mapping.
247+
digToLet[i] = '-';
248+
letToDig.remove(letter);
249+
}
250+
}
251+
}
252+
253+
// If nothing is correct then just return false.
254+
return false;
255+
}
256+
257+
public boolean isSolvable(String[] wordsArr, String result) {
258+
// Add the string 'result' in the list 'words'.
259+
List<String> words = new ArrayList<>();
260+
for (String word : wordsArr) {
261+
words.add(word);
262+
}
263+
words.add(result);
264+
265+
int totalRows = words.size();
266+
267+
// Find the longest string in the list and set 'totalCols' with the size of that string.
268+
int totalCols = 0;
269+
for (String word : words) {
270+
if (totalCols < word.length()) {
271+
totalCols = word.length();
272+
}
273+
}
274+
275+
// Create a HashMap for the letter to digit mapping.
276+
HashMap<Character, Integer> letToDig = new HashMap<>();
277+
278+
// Create a char array for the digit to letter mapping.
279+
char[] digToLet = new char[10];
280+
for (int i = 0; i < 10; i++) {
281+
digToLet[i] = '-';
282+
}
283+
284+
return isAnyMapping(words, 0, 0, 0, letToDig, digToLet, totalRows, totalCols);
285+
}
286+
}
96287
```
97288

98289
#### C++
99290

100291
```cpp
101-
292+
class Solution {
293+
public:
294+
bool isAnyMapping(vector<string>& words, int row, int col, int bal, unordered_map<char, int>& letToDig,
295+
vector<char>& digToLet, int totalRows, int totalCols) {
296+
// If traversed all columns.
297+
if (col == totalCols) {
298+
return bal == 0;
299+
}
300+
301+
// At the end of a particular column.
302+
if (row == totalRows) {
303+
return (bal % 10 == 0 && isAnyMapping(words, 0, col + 1, bal / 10, letToDig, digToLet, totalRows, totalCols));
304+
}
305+
306+
string w = words[row];
307+
308+
// If the current string 'W' has no character in the ('COL')th index.
309+
if (col >= w.length()) {
310+
return isAnyMapping(words, row + 1, col, bal, letToDig, digToLet, totalRows, totalCols);
311+
}
312+
313+
// Take the current character in the variable letter.
314+
char letter = w[w.length() - 1 - col];
315+
316+
// Create a variable 'SIGN' to check whether we have to add it or subtract it.
317+
int sign;
318+
319+
if (row < totalRows - 1) {
320+
sign = 1;
321+
} else {
322+
sign = -1;
323+
}
324+
325+
/*
326+
If we have a prior valid mapping, then use that mapping.
327+
The second condition is for the leading zeros.
328+
*/
329+
if (letToDig.count(letter) && (letToDig[letter] != 0 || (letToDig[letter] == 0 && w.length() == 1) || col != w.length() - 1)) {
330+
331+
return isAnyMapping(words, row + 1, col, bal + sign * letToDig[letter],
332+
letToDig, digToLet, totalRows, totalCols);
333+
334+
}
335+
// Choose a new mapping.
336+
else {
337+
for (int i = 0; i < 10; i++) {
338+
339+
// If 'i'th mapping is valid then select it.
340+
if (digToLet[i] == '-' && (i != 0 || (i == 0 && w.length() == 1) || col != w.length() - 1)) {
341+
digToLet[i] = letter;
342+
letToDig[letter] = i;
343+
344+
// Call the function again with the new mapping.
345+
bool x = isAnyMapping(words, row + 1, col, bal + sign * letToDig[letter],
346+
letToDig, digToLet, totalRows, totalCols);
347+
348+
if (x == true) {
349+
return true;
350+
}
351+
352+
// Unselect the mapping.
353+
digToLet[i] = '-';
354+
if (letToDig.find(letter) != letToDig.end()) {
355+
letToDig.erase(letter);
356+
}
357+
}
358+
}
359+
}
360+
361+
// If nothing is correct then just return false.
362+
return false;
363+
}
364+
365+
bool isSolvable(vector<string>& words, string result) {
366+
// Add the string 'RESULT' in the vector 'WORDS'.
367+
words.push_back(result);
368+
369+
int totalRows;
370+
int totalCols;
371+
372+
// Initialize 'TOTALROWS' with the size of the vector.
373+
totalRows = words.size();
374+
375+
// Find the longest string in the vector and set 'TOTALCOLS' with the size of that string.
376+
totalCols = 0;
377+
378+
for (int i = 0; i < words.size(); i++) {
379+
380+
// If the current string is the longest then update 'TOTALCOLS' with its length.
381+
if (totalCols < words[i].size()) {
382+
totalCols = words[i].size();
383+
}
384+
}
385+
386+
// Create a HashMap for the letter to digit mapping.
387+
unordered_map<char, int> letToDig;
388+
389+
// Create a vector for the digit to letter mapping.
390+
vector<char> digToLet(10, '-');
391+
392+
return isAnyMapping(words, 0, 0, 0, letToDig, digToLet, totalRows, totalCols);
393+
}
394+
};
102395
```
103396

104397
#### Go

0 commit comments

Comments
 (0)