-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgo.py
More file actions
74 lines (71 loc) · 2.28 KB
/
algo.py
File metadata and controls
74 lines (71 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
complexity of this algo O(10^length of unique string)
for example: In the case of Send,More and Money
The Unique string will be SENDMORY,its length is 8,so the complexity is O(10^8)
Also some things are improved from the java algo
"""
class cryp:
def __init__(self) -> None:
self.flag = False
self.resList = []
def makenum(self,map:dict,str:str):
s = ""
for i in str:
s = s + map[i].__str__()
return int(s)
def checkForInitialZeroes(self,words:list,map:dict):
for word in words:
if(map[word[0]]==0):
return True
return False
def helper(self,map:dict,unique:str,used:list,words:list,result:str,target:int)->None:
# base case
if(target==len(unique)):
if(map[result[0]]==0 or self.checkForInitialZeroes(words,map)):
return
sum=0
for i in words:
sum = sum + self.makenum(map,i)
res = self.makenum(map,result)
if(sum==res):
self.flag = True
ans=[]
for i in map:
ans.append(i + ": " +map[i].__str__())
self.resList.append(ans)
return
ch = unique[target]
for i in range(0,10):
if(used[i]==False):
map[ch] = i
used[i] = True
self.helper(map,unique,used,words,result,target+1)
used[i] = False
def isSolvable(self,words:list,result:str)->bool:
map = {}
unique = ""
for i in words:
for j in i:
if((j in map)==False):
map[j] = -1
unique = unique+j
for i in result:
if((i in map)==False):
map[i] = -1
unique =unique + i
print(unique)
if(len(unique)>10):
return False
used=[]
for i in range(0,10):
used.append(False)
self.helper(map,unique,used,words,result,target=0)
print(len(map))
return self.flag
if __name__=='__main__':
words = ["ABCD","EF"]
res = "ABCG"
ob = cryp()
print(ob.isSolvable(words,res))
print(ob.resList)
print(len(ob.resList))