forked from mission-peace/interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringpermutation.py
More file actions
35 lines (30 loc) · 900 Bytes
/
stringpermutation.py
File metadata and controls
35 lines (30 loc) · 900 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
# string permutation in lexicographically order with repetition of characters in the string
def permute(input):
count_map = {}
for ch in input:
if ch in count_map.keys():
count_map[ch] = count_map[ch] + 1
else:
count_map[ch] = 1
keys = sorted(count_map)
str = []
count = []
for key in keys:
str.append(key)
count.append(count_map[key])
result = [0 for x in range(len(input))]
permute_util(str, count, result, 0)
def permute_util(str, count, result, level):
if level == len(result):
print(result)
return
for i in range(len(str)):
if count[i] == 0:
continue;
result[level] = str[i]
count[i] -= 1
permute_util(str, count, result, level + 1)
count[i] += 1
if __name__ == '__main__':
input = ['B', 'C', 'A', 'A']
permute(input)