1
- import java .util .HashMap ;
2
- import java .util .List ;
3
- import java .util .ArrayList ;
4
-
5
- public class Solution {
6
-
7
- private boolean isAnyMapping (List <String > words , int row , int col , int bal ,
8
- HashMap <Character , Integer > letToDig ,
9
- char [] digToLet , int totalRows , int totalCols ) {
1
+ class Solution {
2
+ private boolean isAnyMapping (List <String > words , int row , int col , int bal ,
3
+ HashMap <Character , Integer > letToDig , char [] digToLet , int totalRows , int totalCols ) {
10
4
// If traversed all columns.
11
5
if (col == totalCols ) {
12
6
return bal == 0 ;
13
7
}
14
8
15
9
// At the end of a particular column.
16
10
if (row == totalRows ) {
17
- return (bal % 10 == 0 &&
18
- isAnyMapping (words , 0 , col + 1 , bal / 10 , letToDig , digToLet , totalRows , totalCols ));
11
+ return (bal % 10 == 0
12
+ && isAnyMapping (
13
+ words , 0 , col + 1 , bal / 10 , letToDig , digToLet , totalRows , totalCols ));
19
14
}
20
15
21
16
String w = words .get (row );
@@ -33,23 +28,25 @@ private boolean isAnyMapping(List<String> words, int row, int col, int bal,
33
28
34
29
// If we have a prior valid mapping, then use that mapping.
35
30
// The second condition is for the leading zeros.
36
- if (letToDig .containsKey (letter ) &&
37
- (letToDig .get (letter ) != 0 || (letToDig .get (letter ) == 0 && w .length () == 1 ) || col != w .length () - 1 )) {
38
-
39
- return isAnyMapping (words , row + 1 , col , bal + sign * letToDig .get (letter ),
40
- letToDig , digToLet , totalRows , totalCols );
31
+ if (letToDig .containsKey (letter )
32
+ && (letToDig .get (letter ) != 0 || (letToDig .get (letter ) == 0 && w .length () == 1 )
33
+ || col != w .length () - 1 )) {
34
+
35
+ return isAnyMapping (words , row + 1 , col , bal + sign * letToDig .get (letter ), letToDig ,
36
+ digToLet , totalRows , totalCols );
41
37
42
38
} else {
43
39
// Choose a new mapping.
44
40
for (int i = 0 ; i < 10 ; i ++) {
45
41
// If 'i'th mapping is valid then select it.
46
- if (digToLet [i ] == '-' && (i != 0 || (i == 0 && w .length () == 1 ) || col != w .length () - 1 )) {
42
+ if (digToLet [i ] == '-'
43
+ && (i != 0 || (i == 0 && w .length () == 1 ) || col != w .length () - 1 )) {
47
44
digToLet [i ] = letter ;
48
45
letToDig .put (letter , i );
49
-
46
+
50
47
// Call the function again with the new mapping.
51
- if (isAnyMapping (words , row + 1 , col , bal + sign * letToDig .get (letter ),
52
- letToDig , digToLet , totalRows , totalCols )) {
48
+ if (isAnyMapping (words , row + 1 , col , bal + sign * letToDig .get (letter ),
49
+ letToDig , digToLet , totalRows , totalCols )) {
53
50
return true ;
54
51
}
55
52
@@ -93,20 +90,4 @@ public boolean isSolvable(String[] wordsArr, String result) {
93
90
94
91
return isAnyMapping (words , 0 , 0 , 0 , letToDig , digToLet , totalRows , totalCols );
95
92
}
96
-
97
- public static void main (String [] args ) {
98
- Solution sol = new Solution ();
99
-
100
- String [] words1 = {"SEND" , "MORE" };
101
- String result1 = "MONEY" ;
102
- System .out .println (sol .isSolvable (words1 , result1 )); // Output: true
103
-
104
- String [] words2 = {"SIX" , "SEVEN" , "SEVEN" };
105
- String result2 = "TWENTY" ;
106
- System .out .println (sol .isSolvable (words2 , result2 )); // Output: true
107
-
108
- String [] words3 = {"LEET" , "CODE" };
109
- String result3 = "POINT" ;
110
- System .out .println (sol .isSolvable (words3 , result3 )); // Output: false
111
- }
112
93
}
0 commit comments