29All Permutations

    Hardrecursionbacktracking

    Write permutations(text) that returns a sorted list of every distinct way to rearrange the characters of text.

    Letters can repeat, so "aab" has only 3 distinct permutations: ["aab", "aba", "baa"].

    Examples

    Example 1
    >>> permutations('abc')
    ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
    Example 2
    >>> permutations('aab')
    ['aab', 'aba', 'baa']

    Good to know

    • Write it yourself with recursion; itertools is not needed.

    Run examples checks the 2 examples above. Submit runs all 6 tests, including hidden edge cases, and marks the challenge solved.

    solution.py
    Loading Python…

    Test results show up here

    Write permutations in the editor, then run the examples. Use print() freely: its output is shown for each test.