-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path8.8-Permutations_With_Dups.py
85 lines (67 loc) · 2.5 KB
/
8.8-Permutations_With_Dups.py
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
75
76
77
78
79
80
81
82
83
84
85
# CTCI 8.8
# Permutations With Dups
#-------------------------------------------------------------------------------
# My Solution
#-------------------------------------------------------------------------------
# Only generates new permutations using freq list
#-------------------------------------------------------------------------------
def perm_no_dups(str):
freq_list = build_freq(str)
return perm_help(freq_list, "", len(str), [])
# Build freq list
def build_freq(str):
result = {}
for c in str:
if c in result:
result[c] += 1
else:
result[c] = 1
return result
# Do all perm generation here
def perm_help(freq_list, prefix, remaining, result):
# If all characters used
if remaining == 0:
result.append(prefix)
return result
# Loop through all possible letters to use
for c in freq_list.keys():
count = freq_list[c]
# If # in freq list > 0
if count > 0:
freq_list[c] -= 1
# Add the char to the prefix
# This will generate all possible prefixes
perm_help(freq_list, prefix+c, remaining-1, result)
# Add all the characters back for the next loop
freq_list[c] += 1
return result
#-------------------------------------------------------------------------------
# Less efficient way
# Always runs n! because it generates dups
#-------------------------------------------------------------------------------
def perm(str):
if str == None:
return None
result = []
if len(str) == 0:
result.append("")
return result
first = str[0]
remainder = str[1:len(str)]
# This recursively gets permutations from the remainder, so it starts at the empty string
permutations = perm(remainder)
# For every permutations
for word in permutations:
# For every possible index in each permutation
for i in range(len(word)+1):
# Add the new letter to every index to generate new set of permutations
newperm = insertAt(word, first, i)
if newperm not in result:
result.append(newperm)
return result
def insertAt(str, c, idx):
return str[:idx] + c + str[idx:len(str)]
#-------------------------------------------------------------------------------
#Testing
#-------------------------------------------------------------------------------
print(perm_no_dups("aabb"))