Listing all permutations of a string/integer

后端 未结 29 2688
没有蜡笔的小新
没有蜡笔的小新 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:26

    class Permutation
    {
        public static List Permutate(string seed, List lstsList)
        {
            loopCounter = 0;
            // string s="\w{0,2}";
            var lstStrs = PermuateRecursive(seed);
    
            Trace.WriteLine("Loop counter :" + loopCounter);
            return lstStrs;
        }
    
        // Recursive function to find permutation
        private static List PermuateRecursive(string seed)
        {
            List lstStrs = new List();
    
            if (seed.Length > 2)
            {
                for (int i = 0; i < seed.Length; i++)
                {
                    str = Swap(seed, 0, i);
    
                    PermuateRecursive(str.Substring(1, str.Length - 1)).ForEach(
                        s =>
                        {
                            lstStrs.Add(str[0] + s);
                            loopCounter++;
                        });
                    ;
                }
            }
            else
            {
                lstStrs.Add(seed);
                lstStrs.Add(Swap(seed, 0, 1));
            }
            return lstStrs;
        }
        //Loop counter variable to count total number of loop execution in various functions
        private static int loopCounter = 0;
    
        //Non recursive  version of permuation function
        public static List Permutate(string seed)
        {
            loopCounter = 0;
            List strList = new List();
            strList.Add(seed);
            for (int i = 0; i < seed.Length; i++)
            {
                int count = strList.Count;
                for (int j = i + 1; j < seed.Length; j++)
                {
                    for (int k = 0; k < count; k++)
                    {
                        strList.Add(Swap(strList[k], i, j));
                        loopCounter++;
                    }
                }
            }
            Trace.WriteLine("Loop counter :" + loopCounter);
            return strList;
        }
    
        private static string Swap(string seed, int p, int p2)
        {
            Char[] chars = seed.ToCharArray();
            char temp = chars[p2];
            chars[p2] = chars[p];
            chars[p] = temp;
            return new string(chars);
        }
    }
    

提交回复
热议问题