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