|
| 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) { |
| 4 | + // If traversed all columns. |
| 5 | + if (col == totalCols) { |
| 6 | + return bal == 0; |
| 7 | + } |
| 8 | + |
| 9 | + // At the end of a particular column. |
| 10 | + if (row == totalRows) { |
| 11 | + return (bal % 10 == 0 |
| 12 | + && isAnyMapping( |
| 13 | + words, 0, col + 1, bal / 10, letToDig, digToLet, totalRows, totalCols)); |
| 14 | + } |
| 15 | + |
| 16 | + String w = words.get(row); |
| 17 | + |
| 18 | + // If the current string 'w' has no character in the ('col')th index. |
| 19 | + if (col >= w.length()) { |
| 20 | + return isAnyMapping(words, row + 1, col, bal, letToDig, digToLet, totalRows, totalCols); |
| 21 | + } |
| 22 | + |
| 23 | + // Take the current character in the variable letter. |
| 24 | + char letter = w.charAt(w.length() - 1 - col); |
| 25 | + |
| 26 | + // Create a variable 'sign' to check whether we have to add it or subtract it. |
| 27 | + int sign = (row < totalRows - 1) ? 1 : -1; |
| 28 | + |
| 29 | + // If we have a prior valid mapping, then use that mapping. |
| 30 | + // The second condition is for the leading zeros. |
| 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); |
| 37 | + |
| 38 | + } else { |
| 39 | + // Choose a new mapping. |
| 40 | + for (int i = 0; i < 10; i++) { |
| 41 | + // If 'i'th mapping is valid then select it. |
| 42 | + if (digToLet[i] == '-' |
| 43 | + && (i != 0 || (i == 0 && w.length() == 1) || col != w.length() - 1)) { |
| 44 | + digToLet[i] = letter; |
| 45 | + letToDig.put(letter, i); |
| 46 | + |
| 47 | + // Call the function again with the new mapping. |
| 48 | + if (isAnyMapping(words, row + 1, col, bal + sign * letToDig.get(letter), |
| 49 | + letToDig, digToLet, totalRows, totalCols)) { |
| 50 | + return true; |
| 51 | + } |
| 52 | + |
| 53 | + // Unselect the mapping. |
| 54 | + digToLet[i] = '-'; |
| 55 | + letToDig.remove(letter); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // If nothing is correct then just return false. |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + public boolean isSolvable(String[] wordsArr, String result) { |
| 65 | + // Add the string 'result' in the list 'words'. |
| 66 | + List<String> words = new ArrayList<>(); |
| 67 | + for (String word : wordsArr) { |
| 68 | + words.add(word); |
| 69 | + } |
| 70 | + words.add(result); |
| 71 | + |
| 72 | + int totalRows = words.size(); |
| 73 | + |
| 74 | + // Find the longest string in the list and set 'totalCols' with the size of that string. |
| 75 | + int totalCols = 0; |
| 76 | + for (String word : words) { |
| 77 | + if (totalCols < word.length()) { |
| 78 | + totalCols = word.length(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Create a HashMap for the letter to digit mapping. |
| 83 | + HashMap<Character, Integer> letToDig = new HashMap<>(); |
| 84 | + |
| 85 | + // Create a char array for the digit to letter mapping. |
| 86 | + char[] digToLet = new char[10]; |
| 87 | + for (int i = 0; i < 10; i++) { |
| 88 | + digToLet[i] = '-'; |
| 89 | + } |
| 90 | + |
| 91 | + return isAnyMapping(words, 0, 0, 0, letToDig, digToLet, totalRows, totalCols); |
| 92 | + } |
| 93 | +} |
0 commit comments