From 7b3a50ddc6b907acd6c70e7ccabe7d884a9dd150 Mon Sep 17 00:00:00 2001 From: CodersAcademy006 <104912634+CodersAcademy006@users.noreply.github.com> Date: Fri, 9 Aug 2024 12:25:22 +0530 Subject: [PATCH 1/2] Create Solution.py --- .../1307.Verbal Arithmetic Puzzle/Solution.py | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 solution/1300-1399/1307.Verbal Arithmetic Puzzle/Solution.py diff --git a/solution/1300-1399/1307.Verbal Arithmetic Puzzle/Solution.py b/solution/1300-1399/1307.Verbal Arithmetic Puzzle/Solution.py new file mode 100644 index 0000000000000..3f998cab6dde6 --- /dev/null +++ b/solution/1300-1399/1307.Verbal Arithmetic Puzzle/Solution.py @@ -0,0 +1,87 @@ +class Solution: + def isAnyMapping(self, words, row, col, bal, letToDig, digToLet, totalRows, totalCols): + # If traversed all columns. + if col == totalCols: + return bal == 0 + + # At the end of a particular column. + if row == totalRows: + return (bal % 10 == 0 and + self.isAnyMapping(words, 0, col + 1, bal // 10, letToDig, digToLet, totalRows, totalCols)) + + w = words[row] + + # If the current string 'w' has no character in the ('col')th index. + if col >= len(w): + return self.isAnyMapping(words, row + 1, col, bal, letToDig, digToLet, totalRows, totalCols) + + # Take the current character in the variable letter. + letter = w[len(w) - 1 - col] + + # Create a variable 'sign' to check whether we have to add it or subtract it. + if row < totalRows - 1: + sign = 1 + else: + sign = -1 + + # If we have a prior valid mapping, then use that mapping. + # The second condition is for the leading zeros. + if (letter in letToDig and + (letToDig[letter] != 0 or (letToDig[letter] == 0 and len(w) == 1) or col != len(w) - 1)): + + return self.isAnyMapping(words, row + 1, col, bal + sign * letToDig[letter], + letToDig, digToLet, totalRows, totalCols) + + # Choose a new mapping. + else: + for i in range(10): + # If 'i'th mapping is valid then select it. + if digToLet[i] == '-' and (i != 0 or (i == 0 and len(w) == 1) or col != len(w) - 1): + digToLet[i] = letter + letToDig[letter] = i + + # Call the function again with the new mapping. + if self.isAnyMapping(words, row + 1, col, bal + sign * letToDig[letter], + letToDig, digToLet, totalRows, totalCols): + return True + + # Unselect the mapping. + digToLet[i] = '-' + if letter in letToDig: + del letToDig[letter] + + # If nothing is correct then just return false. + return False + + def isSolvable(self, words, result): + # Add the string 'result' in the list 'words'. + words.append(result) + + # Initialize 'totalRows' with the size of the list. + totalRows = len(words) + + # Find the longest string in the list and set 'totalCols' with the size of that string. + totalCols = max(len(word) for word in words) + + # Create a HashMap for the letter to digit mapping. + letToDig = {} + + # Create a list for the digit to letter mapping. + digToLet = ['-'] * 10 + + return self.isAnyMapping(words, 0, 0, 0, letToDig, digToLet, totalRows, totalCols) + +# Example usage: +sol = Solution() + +words1 = ["SEND", "MORE"] +result1 = "MONEY" +print(sol.isSolvable(words1, result1)) # Output: True + +words2 = ["SIX", "SEVEN", "SEVEN"] +result2 = "TWENTY" +print(sol.isSolvable(words2, result2)) # Output: True + +words3 = ["LEET", "CODE"] +result3 = "POINT" +print(sol.isSolvable(words3, result3)) # Output: False From f742700b556c134809c6a96959e7c361f6e1a3d1 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 13 Aug 2024 08:20:41 +0800 Subject: [PATCH 2/2] Update Solution.py --- .../1307.Verbal Arithmetic Puzzle/Solution.py | 77 +++++++++++-------- 1 file changed, 45 insertions(+), 32 deletions(-) diff --git a/solution/1300-1399/1307.Verbal Arithmetic Puzzle/Solution.py b/solution/1300-1399/1307.Verbal Arithmetic Puzzle/Solution.py index 3f998cab6dde6..7a19318dc8d30 100644 --- a/solution/1300-1399/1307.Verbal Arithmetic Puzzle/Solution.py +++ b/solution/1300-1399/1307.Verbal Arithmetic Puzzle/Solution.py @@ -1,19 +1,24 @@ class Solution: - def isAnyMapping(self, words, row, col, bal, letToDig, digToLet, totalRows, totalCols): + def isAnyMapping( + self, words, row, col, bal, letToDig, digToLet, totalRows, totalCols + ): # If traversed all columns. if col == totalCols: return bal == 0 # At the end of a particular column. if row == totalRows: - return (bal % 10 == 0 and - self.isAnyMapping(words, 0, col + 1, bal // 10, letToDig, digToLet, totalRows, totalCols)) + return bal % 10 == 0 and self.isAnyMapping( + words, 0, col + 1, bal // 10, letToDig, digToLet, totalRows, totalCols + ) w = words[row] # If the current string 'w' has no character in the ('col')th index. if col >= len(w): - return self.isAnyMapping(words, row + 1, col, bal, letToDig, digToLet, totalRows, totalCols) + return self.isAnyMapping( + words, row + 1, col, bal, letToDig, digToLet, totalRows, totalCols + ) # Take the current character in the variable letter. letter = w[len(w) - 1 - col] @@ -26,27 +31,48 @@ def isAnyMapping(self, words, row, col, bal, letToDig, digToLet, totalRows, tota # If we have a prior valid mapping, then use that mapping. # The second condition is for the leading zeros. - if (letter in letToDig and - (letToDig[letter] != 0 or (letToDig[letter] == 0 and len(w) == 1) or col != len(w) - 1)): - - return self.isAnyMapping(words, row + 1, col, bal + sign * letToDig[letter], - letToDig, digToLet, totalRows, totalCols) + if letter in letToDig and ( + letToDig[letter] != 0 + or (letToDig[letter] == 0 and len(w) == 1) + or col != len(w) - 1 + ): + + return self.isAnyMapping( + words, + row + 1, + col, + bal + sign * letToDig[letter], + letToDig, + digToLet, + totalRows, + totalCols, + ) # Choose a new mapping. else: for i in range(10): # If 'i'th mapping is valid then select it. - if digToLet[i] == '-' and (i != 0 or (i == 0 and len(w) == 1) or col != len(w) - 1): + if digToLet[i] == "-" and ( + i != 0 or (i == 0 and len(w) == 1) or col != len(w) - 1 + ): digToLet[i] = letter letToDig[letter] = i - + # Call the function again with the new mapping. - if self.isAnyMapping(words, row + 1, col, bal + sign * letToDig[letter], - letToDig, digToLet, totalRows, totalCols): + if self.isAnyMapping( + words, + row + 1, + col, + bal + sign * letToDig[letter], + letToDig, + digToLet, + totalRows, + totalCols, + ): return True # Unselect the mapping. - digToLet[i] = '-' + digToLet[i] = "-" if letter in letToDig: del letToDig[letter] @@ -65,23 +91,10 @@ def isSolvable(self, words, result): # Create a HashMap for the letter to digit mapping. letToDig = {} - - # Create a list for the digit to letter mapping. - digToLet = ['-'] * 10 - - return self.isAnyMapping(words, 0, 0, 0, letToDig, digToLet, totalRows, totalCols) -# Example usage: -sol = Solution() - -words1 = ["SEND", "MORE"] -result1 = "MONEY" -print(sol.isSolvable(words1, result1)) # Output: True - -words2 = ["SIX", "SEVEN", "SEVEN"] -result2 = "TWENTY" -print(sol.isSolvable(words2, result2)) # Output: True + # Create a list for the digit to letter mapping. + digToLet = ["-"] * 10 -words3 = ["LEET", "CODE"] -result3 = "POINT" -print(sol.isSolvable(words3, result3)) # Output: False + return self.isAnyMapping( + words, 0, 0, 0, letToDig, digToLet, totalRows, totalCols + )