Permutations of a String

Implement a routine that prints all possible orderings of the characters in a string. In other words, print all permutations that use all the characters from the original string. For example, given the string “hat”, your function should print the strings “tha”, “aht”, “tah”, “ath”, “hta”, and “hat”. Treat each character in the input string as a distinct character, even if it is repeated. Given the string “aaa”, your routine should print “aaa” six times. You may print the permutations in any order you choose.

 

using System;
using System.Collections.Generic;
using System.Text;

namespace Permutation
{
    class Permutation
    {
        static void Main(string[] args)
        {
            try
            {
                Console.Write("Enter a word: ");
                string word = Console.ReadLine();
                if (word.Length != 0)
                {
                    //Create an array based on word size
                    char[,] charMatrix = new char[word.Length, word.Length];
                    //Initializing character matrix
                    for (int i = 0; i < word.Length; i++)
                    {
                        for (int j = 0; j < word.Length; j++)
                        {
                            charMatrix[i, j] = char.Parse(word.Substring(j, 1));
                        }
                    }
                    //Create the pointer array
                    int[] pointer = new int[word.Length];
                    //Pointer array initialization
                    for (int i = 0; i < word.Length; i++)
                    {
                        pointer[i] = i;
                    }
                    //Pointer increment loop
                    pointer[word.Length – 1]–;
                    do
                    {
                        int i = word.Length – 1;
                    label1:
                        pointer[i]++;
                        if (pointer[i] == word.Length && i != 0)
                        {
                            pointer[i] = 0;
                            i–;
                            goto label1;
                        }
                        else if (pointer[0] == word.Length)
                        {
                            break;
                        }
                        else
                        {
                            bool valid = true;
                            //Check for valid pointer values
                            int[] check = new int[word.Length];
                            for (int j = 0; j < word.Length; j++)
                            {
                                check[pointer[j]]++;
                                if (check[pointer[j]] > 1)
                                {
                                    valid = false;
                                    break;
                                }
                            }
                            if (valid)
                            {
                                //Print the matrix based on pointer array
                                for (int j = 0; j < word.Length; j++)
                                {
                                    Console.Write(charMatrix[j, pointer[j]]);
                                }
                                Console.WriteLine();
                            }
                        }
                    } while (pointer[0] != word.Length);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
            }
        }
    }
}

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s