-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathStringPermutation.py
52 lines (46 loc) · 1.47 KB
/
StringPermutation.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
#String Permutation Algorithm
#Using Tushar Roy's Backtracking Video
#Prints the permutation
def permute(count, string, result, letterCount):
if letterCount == 0:
print result
return
print count
for i in range(len(string)):
if(count[string[i]] > 0):
count[string[i]] -= 1
permute(count, string, result+string[i], letterCount-1)
#Backtrack
count[string[i]] += 1
#This function compresses the string and fills the count hash
def strCompress(count, string):
#letterCount counts the total number of letters in string
letterCount = 0
for letter in string:
letterCount += 1
if(letter in count):
count[letter] +=1
else:
count[letter] = 1
compressedString = ""
for k,v in count.items():
compressedString += str(k)
return count,compressedString, letterCount
#Supports strings with repeated characters
def bar(string):
count = {}
#Get the character count
count, compressedString, letterCount = strCompress(count, string)
permute(count, compressedString, "", letterCount)
#Does not support strings with repeated characters like AABC
def foo(string, result):
if string == "":
print result
return
#print string
for i in range(len(string)):
#string[:i]+string[i+1:] => delets string at ith position
foo(string[:i]+string[i+1:], result+string[i])
#Main Program
foo("ABCD","")
bar("AABC")