How to generate all permutations of a string in C#

To generate all permutations of a string in C#, you can create a recursive method that generates the permutations. Here's an example:

using System;
using System.Collections.Generic;

public class Permutations
{
    public static void Main(string[] args)
    {
        string input = "abc";
        List<string> result = GeneratePermutations(input);
        
        foreach (string permutation in result)
        {
            Console.WriteLine(permutation);
        }
    }

    public static List<string> GeneratePermutations(string input)
    {
        List<string> result = new List<string>();
        Permute(input, 0, input.Length - 1, result);
        return result;
    }

    private static void Permute(string input, int left, int right, List<string> result)
    {
        if (left == right)
        {
            result.Add(input);
        }
        else
        {
            for (int i = left; i <= right; i++)
            {
                input = Swap(input, left, i);
                Permute(input, left + 1, right, result);
                input = Swap(input, left, i); // backtrack
            }
        }
    }

    private static string Swap(string input, int i, int j)
    {
        char[] chars = input.ToCharArray();
        char temp = chars[i];
        chars[i] = chars[j];
        chars[j] = temp;
        return new string(chars);
    }
}

In this example, the GeneratePermutations() method is the public method that takes the input string and returns a list of permutations. It calls the private Permute() method, which generates permutations recursively.

The Permute() method takes the input string, the left and right indices, and the list of permutations. The base case is when the left index is equal to the right index, in which case the current permutation is added to the result list. In the recursive case, the method iterates through the characters in the string and swaps them to generate new permutations.

The Swap() method is a helper method that swaps characters at two specified indices in the input string.

In the Main() method, the GeneratePermutations() method is called with a sample input string, and the resulting permutations are printed to the console.

  1. Recursive permutation algorithm in C#:

    using System;
    using System.Collections.Generic;
    
    class Program
    {
        static void Main()
        {
            string input = "abc";
            List<string> permutations = GeneratePermutations(input);
            foreach (var permutation in permutations)
            {
                Console.WriteLine(permutation);
            }
        }
    
        static List<string> GeneratePermutations(string input)
        {
            List<string> permutations = new List<string>();
            Permute(input.ToCharArray(), 0, input.Length - 1, permutations);
            return permutations;
        }
    
        static void Permute(char[] array, int left, int right, List<string> permutations)
        {
            if (left == right)
            {
                permutations.Add(new string(array));
            }
            else
            {
                for (int i = left; i <= right; i++)
                {
                    Swap(array, left, i);
                    Permute(array, left + 1, right, permutations);
                    Swap(array, left, i); // Backtrack
                }
            }
        }
    
        static void Swap(char[] array, int i, int j)
        {
            char temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
    }
    
  2. C# code to generate permutations of characters in a string:

    // Code similar to the recursive algorithm mentioned above