Skip to content

Instantly share code, notes, and snippets.

@Kwisses
Created October 2, 2017 23:48
Show Gist options
  • Save Kwisses/569ba36ee3cfea9324ca573ffcff2e17 to your computer and use it in GitHub Desktop.
Save Kwisses/569ba36ee3cfea9324ca573ffcff2e17 to your computer and use it in GitHub Desktop.
string_permutation.py
class StringPermutation:
def __init__(self):
self.dict = {}
self.chars = []
self.count = []
self.result = []
def run(self, string):
for char in string:
self.result.append(char)
if char in self.dict.keys():
self.dict[char] += 1
else:
self.dict[char] = 1
for i, entry in enumerate(self.dict.items()):
self.chars.append(entry[0])
self.count.append(entry[1])
self.permute(0)
def permute(self, level):
if level == len(self.dict):
print(''.join(self.result))
return
for i in range(len(self.dict)):
if self.count[i] == 0:
continue
self.result[level] = self.chars[i]
self.count[i] -= 1
self.permute(level + 1)
self.count[i] += 1
def main():
string = "AABC"
StringPermutation().run(string)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment