|
| 1 | +class Solution { |
| 2 | +public: |
| 3 | + bool isAnyMapping(vector < string > & words, int row, int col, int bal, unordered_map < char, int > & letToDig, |
| 4 | + vector < char > & digToLet, int totalRows, int totalCols) { |
| 5 | + // If traversed all columns. |
| 6 | + if (col == totalCols) { |
| 7 | + return bal == 0; |
| 8 | + } |
| 9 | + |
| 10 | + // At the end of a particular column. |
| 11 | + if (row == totalRows) { |
| 12 | + return (bal % 10 == 0 && |
| 13 | + isAnyMapping(words, 0, col + 1, bal / 10, letToDig, digToLet, totalRows, totalCols)); |
| 14 | + } |
| 15 | + |
| 16 | + string w = words[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[w.length() - 1 - col]; |
| 25 | + |
| 26 | + // Create a variable 'SIGN' to check whether we have to add it or subtract it. |
| 27 | + int sign; |
| 28 | + |
| 29 | + if (row < totalRows - 1) { |
| 30 | + sign = 1; |
| 31 | + } else { |
| 32 | + sign = -1; |
| 33 | + } |
| 34 | + |
| 35 | + /* |
| 36 | + If we have a prior valid mapping, then use that mapping. |
| 37 | + The second condition is for the leading zeros. |
| 38 | + */ |
| 39 | + if (letToDig.count(letter) && |
| 40 | + (letToDig[letter] != 0 || (letToDig[letter] == 0 && w.length() == 1) || col != w.length() - 1)) { |
| 41 | + |
| 42 | + return isAnyMapping(words, row + 1, col, bal + sign * letToDig[letter], |
| 43 | + letToDig, digToLet, totalRows, totalCols); |
| 44 | + |
| 45 | + } |
| 46 | + // Choose a new mapping. |
| 47 | + else { |
| 48 | + for (int i = 0; i < 10; i++) { |
| 49 | + |
| 50 | + // If 'i'th mapping is valid then select it. |
| 51 | + if (digToLet[i] == '-' && (i != 0 || (i == 0 && w.length() == 1) || col != w.length() - 1)) { |
| 52 | + digToLet[i] = letter; |
| 53 | + letToDig[letter] = i; |
| 54 | + |
| 55 | + // Call the function again with the new mapping. |
| 56 | + bool x = isAnyMapping(words, row + 1, col, bal + sign * letToDig[letter], |
| 57 | + letToDig, digToLet, totalRows, totalCols); |
| 58 | + |
| 59 | + if (x == true) { |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | + // Unselect the mapping. |
| 64 | + digToLet[i] = '-'; |
| 65 | + if (letToDig.find(letter) != letToDig.end()){ |
| 66 | + letToDig.erase(letter); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + // If nothing is correct then just return false. |
| 75 | + return false; |
| 76 | +} |
| 77 | + |
| 78 | + bool isSolvable(vector<string>& words, string result) { |
| 79 | + // Add the string 'RESULT' in the vector 'WORDS'. |
| 80 | + words.push_back(result); |
| 81 | + |
| 82 | + int totalRows; |
| 83 | + int totalCols; |
| 84 | + |
| 85 | + // Initialize 'TOTALROWS' with the size of the vector. |
| 86 | + totalRows = words.size(); |
| 87 | + |
| 88 | + // Find the longest string in the vector and set 'TOTALCOLS' with the size of that string. |
| 89 | + totalCols = 0; |
| 90 | + |
| 91 | + for (int i = 0; i < words.size(); i++) { |
| 92 | + |
| 93 | + // If the current string is the longest then update 'TOTALCOLS' with its length. |
| 94 | + if (totalCols < words[i].size()) { |
| 95 | + totalCols = words[i].size(); |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + // Create a HashMap for the letter to digit mapping. |
| 101 | + unordered_map < char, int > letToDig; |
| 102 | + |
| 103 | + // Create a vector for the digit to letter mapping. |
| 104 | + vector < char > digToLet(10, '-'); |
| 105 | + |
| 106 | + return isAnyMapping(words, 0, 0, 0, letToDig, digToLet, totalRows, totalCols); |
| 107 | + |
| 108 | + } |
| 109 | +}; |
0 commit comments