Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to find all the permutation of the string by backtracking using C#?
String permutation using backtracking is a classic algorithm that generates all possible arrangements of characters in a string. The approach works by fixing one character at a time and recursively finding permutations of the remaining characters, then backtracking to explore other possibilities.
The core idea is to swap characters systematically − fix the first position with each character, find permutations of the rest, then backtrack by undoing the swap to try the next possibility.
How Backtracking Works
The algorithm follows these steps −
Fix the first character by trying each character in that position
Recursively find permutations of the remaining substring
Backtrack by undoing the swap to try the next character
Base case − when we reach the end, print the current permutation
Example Implementation
using System;
namespace ConsoleApplication {
public class BackTracking {
public void StringPermutation(string word, int start, int end) {
if (start == end) {
Console.WriteLine(word);
} else {
for (int i = start; i <= end; i++) {
word = Swap(word, start, i);
StringPermutation(word, start + 1, end);
word = Swap(word, start, i); // backtrack
}
}
}
private string Swap(string word, int start, int end) {
char[] arr = word.ToCharArray();
char temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
return new string(arr);
}
}
class Program {
static void Main(string[] args) {
BackTracking b = new BackTracking();
Console.WriteLine("All permutations of 'ABC':");
b.StringPermutation("ABC", 0, 2);
}
}
}
The output of the above code is −
All permutations of 'ABC': ABC ACB BAC BCA CBA CAB
Using List to Store Results
Here's an alternative approach that stores all permutations in a list −
using System;
using System.Collections.Generic;
class PermutationGenerator {
public static List<string> GetAllPermutations(string str) {
List<string> result = new List<string>();
GeneratePermutations(str, 0, str.Length - 1, result);
return result;
}
private static void GeneratePermutations(string str, int start, int end, List<string> result) {
if (start == end) {
result.Add(str);
} else {
for (int i = start; i <= end; i++) {
str = Swap(str, start, i);
GeneratePermutations(str, start + 1, end, result);
str = Swap(str, start, i);
}
}
}
private static string Swap(string str, int i, int j) {
char[] charArray = str.ToCharArray();
char temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
return new string(charArray);
}
static void Main(string[] args) {
string input = "XYZ";
List<string> permutations = GetAllPermutations(input);
Console.WriteLine($"Total permutations of '{input}': {permutations.Count}");
foreach (string perm in permutations) {
Console.WriteLine(perm);
}
}
}
The output of the above code is −
Total permutations of 'XYZ': 6 XYZ XZY YXZ YZX ZYX ZXY
Time and Space Complexity
| Aspect | Complexity | Explanation |
|---|---|---|
| Time Complexity | O(n! × n) | n! permutations, each taking O(n) time to generate |
| Space Complexity | O(n) | Recursion depth is n levels deep |
| Total Permutations | n! | For string of length n, there are n! arrangements |
Conclusion
Backtracking for string permutations systematically explores all possible character arrangements by fixing positions one by one and undoing changes to explore alternatives. This approach generates all n! permutations efficiently using recursion and the swap-backtrack technique.
