Created
August 26, 2020 11:29
-
-
Save abrarShariar/0ab24c562a73144eeb9072202a260060 to your computer and use it in GitHub Desktop.
Get permutations of a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_permutations(input_str): | |
if len(input_str) <= 1: | |
return set([input_str]) | |
all_chars_except_last = input_str[:-1] | |
last_char = input_str[-1:] | |
permutations_of_all_chars_except_last = get_permutations(all_chars_except_last) | |
permutations = set() | |
for perm_str in permutations_of_all_chars_except_last: | |
for position in range(len(all_chars_except_last) + 1): | |
item = ( | |
perm_str[:position] + last_char + perm_str[position:] | |
) | |
permutations.add(item) | |
return permutations |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment