recursion of premutation

PHOTO EMBED

Fri Jun 16 2023 04:10:58 GMT+0000 (Coordinated Universal Time)

Saved by @Souvikdas #python

def get_permutations(string):
    # Base case: If the string is empty, return an empty list
    if len(string) == 0:
        return []

    # Base case: If the string has only one character, return a list with that character
    if len(string) == 1:
        return [string]

    permutations = []  # List to store permutations

    # Iterate through each character in the string
    for i in range(len(string)):
        current_char = string[i]

        # Generate all permutations of the remaining characters
        remaining_chars = string[:i] + string[i+1:]
        remaining_perms = get_permutations(remaining_chars)

        # Append the current character to each permutation of the remaining characters
        for perm in remaining_perms:
            permutations.append(current_char + perm)

    return permutations

# Example usage
input_string = "abc"
permutations_list = get_permutations(input_string)
print(permutations_list)
content_copyCOPY