|
| 1 | +class Solution: |
| 2 | + def isAnyMapping( |
| 3 | + self, words, row, col, bal, letToDig, digToLet, totalRows, totalCols |
| 4 | + ): |
| 5 | + # If traversed all columns. |
| 6 | + if col == totalCols: |
| 7 | + return bal == 0 |
| 8 | + |
| 9 | + # At the end of a particular column. |
| 10 | + if row == totalRows: |
| 11 | + return bal % 10 == 0 and self.isAnyMapping( |
| 12 | + words, 0, col + 1, bal // 10, letToDig, digToLet, totalRows, totalCols |
| 13 | + ) |
| 14 | + |
| 15 | + w = words[row] |
| 16 | + |
| 17 | + # If the current string 'w' has no character in the ('col')th index. |
| 18 | + if col >= len(w): |
| 19 | + return self.isAnyMapping( |
| 20 | + words, row + 1, col, bal, letToDig, digToLet, totalRows, totalCols |
| 21 | + ) |
| 22 | + |
| 23 | + # Take the current character in the variable letter. |
| 24 | + letter = w[len(w) - 1 - col] |
| 25 | + |
| 26 | + # Create a variable 'sign' to check whether we have to add it or subtract it. |
| 27 | + if row < totalRows - 1: |
| 28 | + sign = 1 |
| 29 | + else: |
| 30 | + sign = -1 |
| 31 | + |
| 32 | + # If we have a prior valid mapping, then use that mapping. |
| 33 | + # The second condition is for the leading zeros. |
| 34 | + if letter in letToDig and ( |
| 35 | + letToDig[letter] != 0 |
| 36 | + or (letToDig[letter] == 0 and len(w) == 1) |
| 37 | + or col != len(w) - 1 |
| 38 | + ): |
| 39 | + |
| 40 | + return self.isAnyMapping( |
| 41 | + words, |
| 42 | + row + 1, |
| 43 | + col, |
| 44 | + bal + sign * letToDig[letter], |
| 45 | + letToDig, |
| 46 | + digToLet, |
| 47 | + totalRows, |
| 48 | + totalCols, |
| 49 | + ) |
| 50 | + |
| 51 | + # Choose a new mapping. |
| 52 | + else: |
| 53 | + for i in range(10): |
| 54 | + # If 'i'th mapping is valid then select it. |
| 55 | + if digToLet[i] == "-" and ( |
| 56 | + i != 0 or (i == 0 and len(w) == 1) or col != len(w) - 1 |
| 57 | + ): |
| 58 | + digToLet[i] = letter |
| 59 | + letToDig[letter] = i |
| 60 | + |
| 61 | + # Call the function again with the new mapping. |
| 62 | + if self.isAnyMapping( |
| 63 | + words, |
| 64 | + row + 1, |
| 65 | + col, |
| 66 | + bal + sign * letToDig[letter], |
| 67 | + letToDig, |
| 68 | + digToLet, |
| 69 | + totalRows, |
| 70 | + totalCols, |
| 71 | + ): |
| 72 | + return True |
| 73 | + |
| 74 | + # Unselect the mapping. |
| 75 | + digToLet[i] = "-" |
| 76 | + if letter in letToDig: |
| 77 | + del letToDig[letter] |
| 78 | + |
| 79 | + # If nothing is correct then just return false. |
| 80 | + return False |
| 81 | + |
| 82 | + def isSolvable(self, words, result): |
| 83 | + # Add the string 'result' in the list 'words'. |
| 84 | + words.append(result) |
| 85 | + |
| 86 | + # Initialize 'totalRows' with the size of the list. |
| 87 | + totalRows = len(words) |
| 88 | + |
| 89 | + # Find the longest string in the list and set 'totalCols' with the size of that string. |
| 90 | + totalCols = max(len(word) for word in words) |
| 91 | + |
| 92 | + # Create a HashMap for the letter to digit mapping. |
| 93 | + letToDig = {} |
| 94 | + |
| 95 | + # Create a list for the digit to letter mapping. |
| 96 | + digToLet = ["-"] * 10 |
| 97 | + |
| 98 | + return self.isAnyMapping( |
| 99 | + words, 0, 0, 0, letToDig, digToLet, totalRows, totalCols |
| 100 | + ) |
0 commit comments