Listing all permutations of a string/integer

后端 未结 29 1991
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 00:44

A common task in programming interviews (not from my experience of interviews though) is to take a string or an integer and list every possible permutation.

Is there

29条回答
  •  醉话见心
    2020-11-22 01:33

    The below is my implementation of permutation . Don't mind the variable names, as i was doing it for fun :)

    class combinations
    {
        static void Main()
        {
    
            string choice = "y";
            do
            {
                try
                {
                    Console.WriteLine("Enter word :");
                    string abc = Console.ReadLine().ToString();
                    Console.WriteLine("Combinatins for word :");
                    List final = comb(abc);
                    int count = 1;
                    foreach (string s in final)
                    {
                        Console.WriteLine("{0} --> {1}", count++, s);
                    }
                    Console.WriteLine("Do you wish to continue(y/n)?");
                    choice = Console.ReadLine().ToString();
                }
                catch (Exception exc)
                {
                    Console.WriteLine(exc);
                }
            } while (choice == "y" || choice == "Y");
        }
    
        static string swap(string test)
        {
            return swap(0, 1, test);
        }
    
        static List comb(string test)
        {
            List sec = new List();
            List first = new List();
            if (test.Length == 1) first.Add(test);
            else if (test.Length == 2) { first.Add(test); first.Add(swap(test)); }
            else if (test.Length > 2)
            {
                sec = generateWords(test);
                foreach (string s in sec)
                {
                    string init = s.Substring(0, 1);
                    string restOfbody = s.Substring(1, s.Length - 1);
    
                    List third = comb(restOfbody);
                    foreach (string s1 in third)
                    {
                        if (!first.Contains(init + s1)) first.Add(init + s1);
                    }
    
    
                }
            }
    
            return first;
        }
    
        static string ShiftBack(string abc)
        {
            char[] arr = abc.ToCharArray();
            char temp = arr[0];
            string wrd = string.Empty;
            for (int i = 1; i < arr.Length; i++)
            {
                wrd += arr[i];
            }
    
            wrd += temp;
            return wrd;
        }
    
        static List generateWords(string test)
        {
            List final = new List();
            if (test.Length == 1)
                final.Add(test);
            else
            {
                final.Add(test);
                string holdString = test;
                while (final.Count < test.Length)
                {
                    holdString = ShiftBack(holdString);
                    final.Add(holdString);
                }
            }
    
            return final;
        }
    
        static string swap(int currentPosition, int targetPosition, string temp)
        {
            char[] arr = temp.ToCharArray();
            char t = arr[currentPosition];
            arr[currentPosition] = arr[targetPosition];
            arr[targetPosition] = t;
            string word = string.Empty;
            for (int i = 0; i < arr.Length; i++)
            {
                word += arr[i];
    
            }
    
            return word;
    
        }
    }
    

提交回复
热议问题