A permutation, also called an “arrangement number” or “order, ” is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation.
Source: Mathword(http://mathworld.wolfram.com/Permutation.html)
Below are the permutations of string ABC.
ABC ACB BAC BCA CBA CAB
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.
C#
// C# program to print all // permutations of a given string. using System; class GFG { /** * permutation function * @param str string to calculate permutation for * @param l starting index * @param r end index */ private static void permute(String str, int l, int r) { if (l == r) Console.WriteLine(str); else { for (int i = l; i <= r; i++) { str = swap(str, l, i); permute(str, l + 1, r); str = swap(str, l, i); } } } /** * Swap Characters at position * @param a string value * @param i position 1 * @param j position 2 * @return swapped string */ public static String swap(String a, int i, int j) { char temp; char[] charArray = a.ToCharArray(); temp = charArray[i]; charArray[i] = charArray[j]; charArray[j] = temp; string s = new string(charArray); return s; } // Driver Code public static void Main() { String str = "ABC"; int n = str.Length; permute(str, 0, n - 1); } } // This code is contributed by mits |
chevron_right
filter_none
Output:
ABC ACB BAC BCA CBA CAB
Please refer complete article on Write a program to print all permutations of a given string for more details!
Recommended Posts:
- C# Program for Anagram Substring Search (Or Search for all permutations)
- Program to Print a New Line in C#
- Program to Print Backslash () in C#
- Program to Input Weekday Number and Print the Weekday in C#
- C# Program for Maximum size square sub-matrix with all 1s
- C# Program to Convert the Octal String to an Integer Number
- C# Program to Convert a Binary String to an Integer
- C# Program for Converting Hexadecimal String to Integer
- Program to find absolute value of a given number
- Different Ways to Take Input and Print a Float Value in C#
- C# Program for Naive algorithm for Pattern Searching
- C# Program for KMP Algorithm for Pattern Searching
- C# Program for Count Inversions in an array | Set 1 (Using Merge Sort)
- C# Program for Median of two sorted arrays of same size
- C# Program for Activity Selection Problem | Greedy Algo-1
- C# Program for Subset Sum Problem | DP-25
- C# Program for Dijkstra's shortest path algorithm | Greedy Algo-7
- C# Program for Maximum sum rectangle in a 2D matrix | DP-27
- C# Program for Largest Sum Contiguous Subarray
- C# Program for Minimum number of jumps to reach end

