C Program
#include <stdio.h> #include <string.h> void swap(char *x, char *y) { char temp = *x; *x = *y; *y = temp; } void permute(char *str, int l, int r) { if (l == r) printf("%s\n", str); else { for (int i = l; i <= r; i++) { swap((str + l), (str + i)); permute(str, l + 1, r); swap((str + l), (str + i)); // backtrack } } } int main() { char str[] = "ABC"; int n = strlen(str); permute(str, 0, n - 1); return 0; }
C Output
Input: ABC Output: ABC ACB BAC BCA CBA CAB
C++ Program
#include <iostream> #include <algorithm> using namespace std; void permute(string s, int l, int r) { if (l == r) cout << s << endl; else { for (int i = l; i <= r; i++) { swap(s[l], s[i]); permute(s, l + 1, r); swap(s[l], s[i]); // backtrack } } } int main() { string s = "DOG"; permute(s, 0, s.size() - 1); return 0; }
C++ Output
Input: DOG Output: DOG DGO ODG OGD GOD GDO
JAVA Program
class Main { static void permute(String s, String ans) { if (s.length() == 0) { System.out.println(ans); return; } for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); String ros = s.substring(0, i) + s.substring(i + 1); permute(ros, ans + ch); } } public static void main(String[] args) { String s = "CAT"; permute(s, ""); } }
JAVA Output
Input: CAT Output: CAT CTA ACT ATC TCA TAC
Python Program
def permute(s, ans=""): if len(s) == 0: print(ans) return for i in range(len(s)): ch = s[i] ros = s[:i] + s[i+1:] permute(ros, ans + ch) s = "BAT" permute(s)
Python Output
Input: BAT Output: BAT BTA ABT ATB TBA TAB
In-Depth Explanation
Example
Consider the string "ABC". There are 3! = 6 permutations in all. We produce them by holding each character in place in turn and permuting the others. So, hold 'A' in place, arrange "BC" → "ABC", "ACB". Hold 'B' in place, arrange "AC" → "BAC", "BCA". Finally, hold 'C' in place, arrange "AB" → "CAB", "CBA". The recursive method automatically accommodates this decomposition.
Real-Life Analogy
Consider three friends (Alice, Bob, and Charlie) standing in line for a photo. The various orders they can stand in line is the permutations of "ABC". Each arrangement yields a different picture, and rearranging their positions is equivalent to rearranging characters in the string. Just as photographers prefer to view all the possible seating arrangements for inspiration, programmers employ permutations to view all possible means data may be ordered.
Why It Matters
Permutations are not only theoretical. They underlie a wide variety of useful computer science problems, such as test case generation, puzzle solutions (such as Sudoku or the "word jumble"), password strength checking, or optimal arrangements in scheduling and transportation planning. In algorithm design, permutations introduce the principles of recursion and backtracking—impressive instruments for partitioning problems and preventing redundant effort.
Learning Insights
The recursive solution is based on the method of holding one character constant and permuting the remaining ones, and backtracking to reverse modifications so the algorithm visits all branches. Backtracking is the key to this approach: it prevents us from losing information when backtracking. Indeed, comprehension of this trick prepares a starter for more complicated problems such as the N-Queens problem, solving the maze, or generating subsets. Iterative solutions with next_permutation() (such as in C++) are possible too, but recursion makes you realize the logic at a basic level.
Interview Relevance
This is an extremely popular question in coding interviews due to the fact that it challenges understanding of recursion, string operations, and algorithmic thinking. Interviewers may begin with "create all permutations of a string" and place restrictions such as "only unique permutations," "lexicographical order," or "permutations of numbers." Staying up to date with this topic reinforces understanding of recursion, one of the most asked topics during technical rounds.
SEO-Optimized Closing
Recognizing how to create permutations of a string via recursion and backtracking is fundamental for interviewees and students rehearsing for coding interviews. This topic is a gateway to solving more challenging algorithm and data structure problems. Practicing permutation programming in C, C++, Java, and Python helps beginners solidify their understanding of recursion and enhance problem-solving. Whether competitive programming, coding interviews, or actual projects where there is a need to arrange and generate combinations, permutation problems are a well-established foundation in logical thinking and programming mastery.
Social Plugin