So we have a (static) map that specifies letters we want to replace with special characters. Except we don't want to return just one string with all the replacements, we want to return every permutation achievable by replacing one or more characters of our original password. Here is one such map:
{'i': '!', 'a': '@', 's': '$', 'o': '0', 'E': '3'}
Here is what I have so far in Python:
def permute_password(password: str, chars_map: dict) -> List[str]:
def find(password, seen, ans):
if len(password)==0:
ans.append(seen)
return
for i in range(len(password)):
pass_cpy=password.copy()
if chars_map.get(pass_cpy[i]):
pass_cpy[i] = chars_map.get(pass_cpy[i])
find(pass_cpy, seen+pass_cpy[i]+pass_cpy[i+1:], ans)
return ans
ans=[]
return find(password, "", ans)
From what I can tell, my problem lies in the for loop. Specifically, I'm not sure how to set up the recursion.
And this is what it would like to run the function:
special_chars = {'i': '!', 'a': '@', 's': '$', 'o': '0', 'E': '3'}
print('\n'.join(permute_password("password", special_chars)))
And this is the desired output:
p@ssword
p@$sword
pa$sword
p@s$word
p@$$word
pa$$word
pas$word
p@ssw0rd
p@$sw0rd
pa$sw0rd
p@s$w0rd
p@$$w0rd
pa$$w0rd
pas$w0rd
passw0rd