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