Webhun Blog
Webhun will try to help on variety of technologies. We hope this will become a good source for their troubled computer problems.

String Permutations in C#

August 7, 2010 05:37 by ZeroInfinity
Bookmark and Share

I come a cross permutation question from a contract job. It wondered me and I decided to take a look at and solve it.

 I come up with two ways to solve this. It may not be the best solution but it works. I would like to hear from others what they thing about it as well.

 

     class Program

    {

        static void Main(string[] args)

        {

            string org = "abcdefght";

            Dictionary<string,char> dic = new Dictionary<string,char>();

            int main = org.Length-1;

            char key = org[main];

            string tmp = org.Remove(main, 1);

            for (int i = 0; i < tmp.Length; i++)

            {

                dic.Add(tmp,key);

                var tmp1 = tmp[0];

                tmp = tmp.Substring(1, tmp.Length - 1) + tmp1;

 

            }

 

            Perms(org, dic, main);

            //Perm(org, org[0],org.Substring(1,org.Length-1),0,org.Substring(1,org.Length-1).Length,false);

        }

 

        public static void Perm(string original,char s, string tmp,int index, int i, bool finished)

        {

                

                

                    if (index <original.Length)

                    {

                        if (i > 0)

                        {

                            Console.WriteLine(s + tmp);

                            i--;

                            var t = tmp[0];

                            var tt = tmp.Substring(1, tmp.Length - 1);

                            tt = tt + t;

                            Perm(original, s, tt, index, i, finished);

                        }

                        else

                        {

                            if (index + 1 < original.Length)

                            {

                                tmp = original.Remove(index + 1, 1);

                                string tmp1 = tmp + s;

                                index++;

                                s = original[index];

 

                                Perm(original, s, tmp, index, original.Length - 1, false);

                            }

                        }

                    }

                

                Console.ReadLine();

       }

        

        public static void Perms(string org,Dictionary<string,char> dic, int main)

        {

            if (main>=0)

            {

                    foreach (string key in dic.Keys)

                    {

                        Console.WriteLine(key + dic[key].ToString());

                    }

                    dic.Clear();

                    if (--main >= 0)

                    {

                        char keyt = org[main];

                        string tmp = org.Remove(main, 1);

                        for (int i = 0; i < tmp.Length; i++)

                        {

                            dic.Add(tmp, keyt);

                            var tmp1 = tmp[0];

                            tmp = tmp.Substring(1, tmp.Length - 1) + tmp1;

                        }

                        Perms(org, dic, main);

                    }

            }

            Console.ReadLine();

        }

 

   }

 

Please let me know what you guys think. Thanks. 


Currently rated 3.5 by 2 people

  • Currently 3.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Surv it Surv it Shout it
Bookmark and Share