@@ -83,19 +83,312 @@ Note that two different characters cannot map to the same digit.
83
83
#### Python3
84
84
85
85
``` python
86
-
86
+ class Solution :
87
+ def isAnyMapping (
88
+ self , words , row , col , bal , letToDig , digToLet , totalRows , totalCols
89
+ ):
90
+ # If traversed all columns.
91
+ if col == totalCols:
92
+ return bal == 0
93
+
94
+ # At the end of a particular column.
95
+ if row == totalRows:
96
+ return bal % 10 == 0 and self .isAnyMapping(
97
+ words, 0 , col + 1 , bal // 10 , letToDig, digToLet, totalRows, totalCols
98
+ )
99
+
100
+ w = words[row]
101
+
102
+ # If the current string 'w' has no character in the ('col')th index.
103
+ if col >= len (w):
104
+ return self .isAnyMapping(
105
+ words, row + 1 , col, bal, letToDig, digToLet, totalRows, totalCols
106
+ )
107
+
108
+ # Take the current character in the variable letter.
109
+ letter = w[len (w) - 1 - col]
110
+
111
+ # Create a variable 'sign' to check whether we have to add it or subtract it.
112
+ if row < totalRows - 1 :
113
+ sign = 1
114
+ else :
115
+ sign = - 1
116
+
117
+ # If we have a prior valid mapping, then use that mapping.
118
+ # The second condition is for the leading zeros.
119
+ if letter in letToDig and (
120
+ letToDig[letter] != 0
121
+ or (letToDig[letter] == 0 and len (w) == 1 )
122
+ or col != len (w) - 1
123
+ ):
124
+
125
+ return self .isAnyMapping(
126
+ words,
127
+ row + 1 ,
128
+ col,
129
+ bal + sign * letToDig[letter],
130
+ letToDig,
131
+ digToLet,
132
+ totalRows,
133
+ totalCols,
134
+ )
135
+
136
+ # Choose a new mapping.
137
+ else :
138
+ for i in range (10 ):
139
+ # If 'i'th mapping is valid then select it.
140
+ if digToLet[i] == " -" and (
141
+ i != 0 or (i == 0 and len (w) == 1 ) or col != len (w) - 1
142
+ ):
143
+ digToLet[i] = letter
144
+ letToDig[letter] = i
145
+
146
+ # Call the function again with the new mapping.
147
+ if self .isAnyMapping(
148
+ words,
149
+ row + 1 ,
150
+ col,
151
+ bal + sign * letToDig[letter],
152
+ letToDig,
153
+ digToLet,
154
+ totalRows,
155
+ totalCols,
156
+ ):
157
+ return True
158
+
159
+ # Unselect the mapping.
160
+ digToLet[i] = " -"
161
+ if letter in letToDig:
162
+ del letToDig[letter]
163
+
164
+ # If nothing is correct then just return false.
165
+ return False
166
+
167
+ def isSolvable (self , words , result ):
168
+ # Add the string 'result' in the list 'words'.
169
+ words.append(result)
170
+
171
+ # Initialize 'totalRows' with the size of the list.
172
+ totalRows = len (words)
173
+
174
+ # Find the longest string in the list and set 'totalCols' with the size of that string.
175
+ totalCols = max (len (word) for word in words)
176
+
177
+ # Create a HashMap for the letter to digit mapping.
178
+ letToDig = {}
179
+
180
+ # Create a list for the digit to letter mapping.
181
+ digToLet = [" -" ] * 10
182
+
183
+ return self .isAnyMapping(
184
+ words, 0 , 0 , 0 , letToDig, digToLet, totalRows, totalCols
185
+ )
87
186
```
88
187
89
188
#### Java
90
189
91
190
``` java
92
-
191
+ class Solution {
192
+ private boolean isAnyMapping (List<String > words , int row , int col , int bal ,
193
+ HashMap<Character , Integer > letToDig , char [] digToLet , int totalRows , int totalCols ) {
194
+ // If traversed all columns.
195
+ if (col == totalCols) {
196
+ return bal == 0 ;
197
+ }
198
+
199
+ // At the end of a particular column.
200
+ if (row == totalRows) {
201
+ return (bal % 10 == 0
202
+ && isAnyMapping(
203
+ words, 0 , col + 1 , bal / 10 , letToDig, digToLet, totalRows, totalCols));
204
+ }
205
+
206
+ String w = words. get(row);
207
+
208
+ // If the current string 'w' has no character in the ('col')th index.
209
+ if (col >= w. length()) {
210
+ return isAnyMapping(words, row + 1 , col, bal, letToDig, digToLet, totalRows, totalCols);
211
+ }
212
+
213
+ // Take the current character in the variable letter.
214
+ char letter = w. charAt(w. length() - 1 - col);
215
+
216
+ // Create a variable 'sign' to check whether we have to add it or subtract it.
217
+ int sign = (row < totalRows - 1 ) ? 1 : - 1 ;
218
+
219
+ // If we have a prior valid mapping, then use that mapping.
220
+ // The second condition is for the leading zeros.
221
+ if (letToDig. containsKey(letter)
222
+ && (letToDig. get(letter) != 0 || (letToDig. get(letter) == 0 && w. length() == 1 )
223
+ || col != w. length() - 1 )) {
224
+
225
+ return isAnyMapping(words, row + 1 , col, bal + sign * letToDig. get(letter), letToDig,
226
+ digToLet, totalRows, totalCols);
227
+
228
+ } else {
229
+ // Choose a new mapping.
230
+ for (int i = 0 ; i < 10 ; i++ ) {
231
+ // If 'i'th mapping is valid then select it.
232
+ if (digToLet[i] == ' -'
233
+ && (i != 0 || (i == 0 && w. length() == 1 ) || col != w. length() - 1 )) {
234
+ digToLet[i] = letter;
235
+ letToDig. put(letter, i);
236
+
237
+ // Call the function again with the new mapping.
238
+ if (isAnyMapping(words, row + 1 , col, bal + sign * letToDig. get(letter),
239
+ letToDig, digToLet, totalRows, totalCols)) {
240
+ return true ;
241
+ }
242
+
243
+ // Unselect the mapping.
244
+ digToLet[i] = ' -' ;
245
+ letToDig. remove(letter);
246
+ }
247
+ }
248
+ }
249
+
250
+ // If nothing is correct then just return false.
251
+ return false ;
252
+ }
253
+
254
+ public boolean isSolvable (String [] wordsArr , String result ) {
255
+ // Add the string 'result' in the list 'words'.
256
+ List<String > words = new ArrayList<> ();
257
+ for (String word : wordsArr) {
258
+ words. add(word);
259
+ }
260
+ words. add(result);
261
+
262
+ int totalRows = words. size();
263
+
264
+ // Find the longest string in the list and set 'totalCols' with the size of that string.
265
+ int totalCols = 0 ;
266
+ for (String word : words) {
267
+ if (totalCols < word. length()) {
268
+ totalCols = word. length();
269
+ }
270
+ }
271
+
272
+ // Create a HashMap for the letter to digit mapping.
273
+ HashMap<Character , Integer > letToDig = new HashMap<> ();
274
+
275
+ // Create a char array for the digit to letter mapping.
276
+ char [] digToLet = new char [10 ];
277
+ for (int i = 0 ; i < 10 ; i++ ) {
278
+ digToLet[i] = ' -' ;
279
+ }
280
+
281
+ return isAnyMapping(words, 0 , 0 , 0 , letToDig, digToLet, totalRows, totalCols);
282
+ }
283
+ }
93
284
```
94
285
95
286
#### C++
96
287
97
288
``` cpp
98
-
289
+ class Solution {
290
+ public:
291
+ bool isAnyMapping(vector<string >& words, int row, int col, int bal, unordered_map<char, int>& letToDig,
292
+ vector<char >& digToLet, int totalRows, int totalCols) {
293
+ // If traversed all columns.
294
+ if (col == totalCols) {
295
+ return bal == 0;
296
+ }
297
+
298
+ // At the end of a particular column.
299
+ if (row == totalRows) {
300
+ return (bal % 10 == 0 && isAnyMapping(words, 0, col + 1, bal / 10, letToDig, digToLet, totalRows, totalCols));
301
+ }
302
+
303
+ string w = words[row];
304
+
305
+ // If the current string 'W' has no character in the ('COL')th index.
306
+ if (col >= w.length()) {
307
+ return isAnyMapping(words, row + 1, col, bal, letToDig, digToLet, totalRows, totalCols);
308
+ }
309
+
310
+ // Take the current character in the variable letter.
311
+ char letter = w[w.length() - 1 - col];
312
+
313
+ // Create a variable 'SIGN' to check whether we have to add it or subtract it.
314
+ int sign;
315
+
316
+ if (row < totalRows - 1) {
317
+ sign = 1;
318
+ } else {
319
+ sign = -1;
320
+ }
321
+
322
+ /*
323
+ If we have a prior valid mapping, then use that mapping.
324
+ The second condition is for the leading zeros.
325
+ */
326
+ if (letToDig.count(letter) && (letToDig[letter] != 0 || (letToDig[letter] == 0 && w.length() == 1) || col != w.length() - 1)) {
327
+
328
+ return isAnyMapping(words, row + 1, col, bal + sign * letToDig[letter],
329
+ letToDig, digToLet, totalRows, totalCols);
330
+
331
+ }
332
+ // Choose a new mapping.
333
+ else {
334
+ for (int i = 0; i < 10; i++) {
335
+
336
+ // If 'i'th mapping is valid then select it.
337
+ if (digToLet[i] == '-' && (i != 0 || (i == 0 && w.length() == 1) || col != w.length() - 1)) {
338
+ digToLet[i] = letter;
339
+ letToDig[letter] = i;
340
+
341
+ // Call the function again with the new mapping.
342
+ bool x = isAnyMapping(words, row + 1, col, bal + sign * letToDig[letter],
343
+ letToDig, digToLet, totalRows, totalCols);
344
+
345
+ if (x == true) {
346
+ return true;
347
+ }
348
+
349
+ // Unselect the mapping.
350
+ digToLet[i] = '-';
351
+ if (letToDig.find(letter) != letToDig.end()) {
352
+ letToDig.erase(letter);
353
+ }
354
+ }
355
+ }
356
+ }
357
+
358
+ // If nothing is correct then just return false.
359
+ return false;
360
+ }
361
+
362
+ bool isSolvable(vector<string>& words, string result) {
363
+ // Add the string 'RESULT' in the vector 'WORDS'.
364
+ words.push_back(result);
365
+
366
+ int totalRows;
367
+ int totalCols;
368
+
369
+ // Initialize 'TOTALROWS' with the size of the vector.
370
+ totalRows = words.size();
371
+
372
+ // Find the longest string in the vector and set 'TOTALCOLS' with the size of that string.
373
+ totalCols = 0;
374
+
375
+ for (int i = 0; i < words.size(); i++) {
376
+
377
+ // If the current string is the longest then update 'TOTALCOLS' with its length.
378
+ if (totalCols < words[i].size()) {
379
+ totalCols = words[i].size();
380
+ }
381
+ }
382
+
383
+ // Create a HashMap for the letter to digit mapping.
384
+ unordered_map<char, int> letToDig;
385
+
386
+ // Create a vector for the digit to letter mapping.
387
+ vector<char> digToLet(10, '-');
388
+
389
+ return isAnyMapping(words, 0, 0, 0, letToDig, digToLet, totalRows, totalCols);
390
+ }
391
+ };
99
392
```
100
393
101
394
#### Go
0 commit comments