![]() |
VOOZH | about |
Given a string s. Find all permutations of a given string. Return the permutations in lexicographically non-decreasing order.
Examples:
Input: s = "ABC"
Output: ["ABC", "ACB", "BAC", "BCA", "CAB", "CBA"]Input: s = "XY"
Output: ["XY", "YX"]Input: s = "AAA"
Output: "AAA", "AAA", "AAA", "AAA", "AAA", "AAA"
Generate permutations by fixing one position at a time. First, we fix the first position and try every character in that position, then recursively generate all permutations for the remaining positions. After we fix the first position, we recursive repeat the process for the remaining string. Once we generate all permutations beginning with the current character, to bring the next character at its position in current string, we swap the next character, with the current character.
AAB AAB ABA ABA BAA BAA
Related articles: