Skip to content

Commit c04fe0e

Browse files
authored
Update Solution.java
1 parent 7267a99 commit c04fe0e

File tree

1 file changed

+17
-36
lines changed

1 file changed

+17
-36
lines changed

solution/1300-1399/1307.Verbal Arithmetic Puzzle/Solution.java

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
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) {
104
// If traversed all columns.
115
if (col == totalCols) {
126
return bal == 0;
137
}
148

159
// At the end of a particular column.
1610
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));
1914
}
2015

2116
String w = words.get(row);
@@ -33,23 +28,25 @@ private boolean isAnyMapping(List<String> words, int row, int col, int bal,
3328

3429
// If we have a prior valid mapping, then use that mapping.
3530
// 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);
4137

4238
} else {
4339
// Choose a new mapping.
4440
for (int i = 0; i < 10; i++) {
4541
// 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)) {
4744
digToLet[i] = letter;
4845
letToDig.put(letter, i);
49-
46+
5047
// 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)) {
5350
return true;
5451
}
5552

@@ -93,20 +90,4 @@ public boolean isSolvable(String[] wordsArr, String result) {
9390

9491
return isAnyMapping(words, 0, 0, 0, letToDig, digToLet, totalRows, totalCols);
9592
}
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-
}
11293
}

0 commit comments

Comments
 (0)