Combinations of a String

Implement a function that prints all possible combinations of the characters in a string. These combinations range in length from one to the length of the string. Two combinations that differ only in ordering of their characters are the same combination. In other words, “12” and “31” are different combinations from the input string “123”, but “21” is the same as “12”.

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

namespace Combinations
{
    class Combinations
    {
        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] = -1;
                    }
                    //Pointer increment loop
                    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 for removing duplicate combinations
                                if (j < word.Length – 1)
                                {
                                    if (pointer[j] > pointer[j + 1] && pointer[j] != -1)
                                    {
                                        valid = false;
                                        break;
                                    }
                                }
                                //check for removing character repetition
                                if (pointer[j] >= 0)
                                {
                                    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++)
                                {
                                    if (pointer[j] >= 0)
                                    {
                                        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