forked from netsetos/python_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutation of String
More file actions
35 lines (31 loc) · 793 Bytes
/
Permutation of String
File metadata and controls
35 lines (31 loc) · 793 Bytes
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
def permute(arr):
dict = {}
for i in arr:
if i in dict.keys():
dict[i] = dict[i] + 1
else:
dict[i] = 1
keys = sorted(dict)
str = []
count = []
start=0
for key in keys:
str.append(key)
count.append(dict[key])
result = [0]*len(arr)
print(str, count, result, start)
permutation(str, count, result, start)
def permutation(str, count, result, start):
if start == len(result):
print(result)
return
for i in range(len(str)):
if count[i] == 0:
continue
result[start] = str[i]
count[i] -= 1
permutation(str, count, result, start + 1)
count[i] += 1
if __name__ == '__main__':
input = ['A', 'A', 'B','C']
permute(input)