Get all permutations of a string

This problem asks you to write a function that returns all possible permutations of a given string. For example, given the input string "abc", the function should return the following six permutations : "abc", "acb", "bac", "bca", "cab", and "cba".

Problem

Implement a function that returns all possible permutations of a given string.
Example input: "abc"
Example output: ["abc", "acb", "bac", "bca", "cab", "cba"]

Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by kayakane
def permutations(string):
    if len(string) == 1:
        return [string]
    else:
        result = []
        for i in range(len(string)):
            for perm in permutations(string[:i] + string[i+1:]):
                result.append(string[i] + perm)
        return result

print(permutations("abc"))

A.I. Evaluation of the Solution

This solution is complete and solves the problem. The approach is to generate all permutations by looping through the characters in the string and then recursively generating all permutations of the remaining characters.

Evaluated at: 2022-12-10 14:15:31