I am a beginner trying to learn recursion in Python. I want to print all the permutations of a given string. For example:
Input: AABC
Output: AABC,AACB,ABAC,ABCA,ACAB,BAAC,BACA,BCAA,CAAB,CABA,CBAA
I have written the following code in Python using recursion.
def foo(str,count):
result = ""
while(any(count)):
for i in range(len(count)):
if count[i]>0:
count[i] -= 1
result = str[i] + foo(str,count)
print(result)
return result
s = "AABC"
n = 4
c = {}
for i in range(len(s)):
if s[i] in c:
c[s[i]] += 1
else:
c[s[i]] = 1
str = list(c.keys())
count = list(c.values())
print(str,count)
foo(str,count)
print(count)
I am getting the output as follows:
['A', 'B', 'C'] [2, 1, 1]
C
BC
ABC
AABC
[0, 0, 0]
It implies that the code is handling only the first case at every level. How can I correct this code?
Any help would be wonderful.
Thanks for your time :)