VOOZH about

URL: https://www.geeksforgeeks.org/dsa/write-a-c-program-to-print-all-permutations-of-a-given-string/

⇱ Permutations of given String - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Permutations of given String

Last Updated : 19 May, 2026

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"

[Approach] Recursion and Swapping - O(n! * n) Time

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.

👁 4

Output
AAB AAB ABA ABA BAA BAA 

Related articles:

Comment