# This function will swap characters for every permutation def swap_char(word, i, j): swap_index = list(word) swap_index[i], swap_index[j] = swap_index[j], swap_index[i] return ''.join(swap_index) def permute_string_rec(word, current_index, result): # Prevents adding duplicate permutations if current_index == 1: result.append(word) return for index in range(current_index, len(word)): # swaps character for each permutation swapped_str = swap_char(word, current_index, index) print("\t After swapping the indices in the string: ", swapped_str, ", index: ", index, sep="") # recursively calls itself to find each permutation permute_string_rec(swapped_str, current_index + 1, result) # Driver code def main(): input_word = ["ab", "bad", "abcd"] result = [] for index in range(len(input_word)): print(index + 1, ".\t Input string: '", input_word[index], "'", sep="") permute_string_rec(input_word[index], 0, result) print('-' * 100) if __name__ == '__main__': main()