Beispiel #1
0
    public static void QuestionsAndSolutionsByChapters()
    {
        var chapters = new[]
        {
            //// Intro
            //new IQuestion[] { new CompareBinaryToHex(), new SwapMinMax(), },

            //// Chapters
            new IQuestion[] {
                new Array_Q1_01_Is_String_Has_Unique_Chars(),
                //,  new Q1_02_Check_Permutation(), new Q1_03_URLify(), new Q1_04_Palindrome_Permutation(), new Q1_05_One_Away_A(), new Q1_06_String_Compression(), new Q1_07_Rotate_Matrix(), new Q1_08_Zero_Matrix(), new Q1_09_String_Rotation(),
            },

            //new IQuestion[] { new Q2_01_Remove_Dups(), new Q2_02_Return_Kth_To_Last(), new Q2_03_Delete_Middle_Node(), new Q2_04_Partition(), new Q2_05_Sum_Lists(), new Q2_06_Palindrome(), new Q2_07_Intersection(), new Q2_08_Loop_Detection() },

            //new IQuestion[] { new Q5_01_Insertion(), new Q5_02_Binary_to_String(), new Q5_04_Next_Number(), new Q5_06_Conversion(), new Q5_06_Conversion(), new Q5_07_Pairwise_Swap(), new Q5_08_Draw_Line() },

            //new IQuestion[] { new Q10_01_Sorted_Merge(), new Q10_02_Group_Anagrams(), new Q10_03_Search_in_Rotated_Array(), new Q10_05_Sparse_Search(), new Q10_08_Find_Duplicates(), new Q10_09_Sorted_Matrix_Search(), new Q10_10_Rank_from_Stream(), new Q10_11_Peaks_and_Valleys() },
        };

        foreach (var chapter in chapters)
        {
            foreach (IQuestion q in chapter)
            {
                Console.WriteLine(string.Format("{0}{1}", Environment.NewLine, Environment.NewLine));
                AssortedMethods.PrintType(q);
                q.Run();
            }

            Console.WriteLine();
            Console.WriteLine("Press Enter to continue..");
            //Console.ReadLine();
        }
    }
Beispiel #2
0
        /// <summary>
        /// Requires no additional memory if used proper sorting algorithms (-which does not require additional memory)
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        public static bool CheckIfStringsArePermutaion_1_UsingSort(string str1, string str2)
        {
            AssortedMethods.PrintType(type: typeof(StringUtility), methodName: nameof(CheckIfStringsArePermutaion_1_UsingSort));

            if (str1.Length != str2.Length)
            {
                Console.WriteLine($"Length of '{str1}' does not match with {str2}");
                return(false);
            }

            var originalAsArray = str1.ToCharArray();

            // Array.Sort Is Default Implementaion.
            // It should be implementted using quick sort see sorting.cs for quick sort exampled
            Array.Sort(originalAsArray);
            //Console.WriteLine($"'{str1}' after sort {new string(originalAsArray)}");
            str1 = new string(originalAsArray);

            var valueToTestAsArray = str2.ToCharArray();

            // Array.Sort Is Default Implementaion.
            // It should be implementted using quick sort see sorting.cs for quick sort exampled
            Array.Sort(valueToTestAsArray);
            //Console.WriteLine($"'{str2}' after sort {new string(valueToTestAsArray)}");
            str2 = new string(valueToTestAsArray);

            Console.WriteLine($" Is '{str1}' and {str2} are permutaion-{str1.Equals(str2)}");
            //Console.WriteLine("===================================================================================");
            return(str1.Equals(str2));
        }
Beispiel #3
0
        /// <summary>
        /// Used for unicodes charctes
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        public static bool CheckIfStringsArePermutaion_3_UsingHashtable(string str1, string str2)
        {
            AssortedMethods.PrintType(type: typeof(StringUtility), methodName: nameof(CheckIfStringsArePermutaion_3_UsingHashtable));
            if (str1.Length != str2.Length)
            {
                //Console.WriteLine($"Length of '{str1}' does not match with {str2}");
                return(false);
            }

            Hashtable hashtable       = new Hashtable();
            var       originalAsArray = str1.ToCharArray();

            originalAsArray.Print($"'{str1}'- original.ToCharArray():");

            foreach (var character in originalAsArray)
            {
                if (hashtable.ContainsKey(character))
                {
                    hashtable[character] = (int)hashtable[character] + 1;
                }
                else
                {
                    hashtable.Add(character, 1);
                }
                //Console.WriteLine($"character -'{character}'; letters[character]-'{letters[character]}'");
            }

            var str2AsArray = str2.ToCharArray();

            str2AsArray.Print($"'{str2}'- valueToTest.ToCharArray():");
            foreach (var character in str2AsArray)
            {
                if (hashtable.ContainsKey(character))
                {
                    hashtable[character] = (int)hashtable[character] - 1;
                }
                else
                {
                    Console.WriteLine($"character= '{character}' does not exists in Hashtable");
                    return(false);
                }

                //why it is -1 vs 0 condition?
                if ((int)hashtable[character] < 0)
                {
                    Console.WriteLine($"character== {character}, (int)hashtable[character] = {(int)hashtable[character]} : (letters[character] < 0 ) = {(int)hashtable[character] < 0}");
                    return(false);
                }
            }

            Console.WriteLine($" Is '{str1}' and {str2} are permutaion= true");
            Console.WriteLine("===================================================================================");

            return(true);
        }
Beispiel #4
0
        /// <summary>
        /// Requires additional memory to store identical counts
        /// assumbed it has only ASCII charectes
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        public static bool CheckIfStringsArePermutaion_2_UsingIdenticalCharecterCount(string str1, string str2)
        {
            AssortedMethods.PrintType(type: typeof(StringUtility), methodName: nameof(CheckIfStringsArePermutaion_2_UsingIdenticalCharecterCount));

            if (str1.Length != str2.Length)
            {
                Console.WriteLine($"Length of '{str1}' does not match with {str2}");
                return(false);
            }

            var letters         = new int[256];
            var originalAsArray = str1.ToCharArray();

            originalAsArray.Print($"'{str1}'- original.ToCharArray():");

            foreach (var character in originalAsArray)
            {
                letters[character]++;
                //Console.WriteLine($"character -'{character}'; letters[character]-'{letters[character]}'");
            }

            //letters.Print($"'{str1}'-");

            var valueToTestAsArray = str2.ToCharArray();

            valueToTestAsArray.Print($"'{str2}'- valueToTest.ToCharArray():");
            foreach (var character in valueToTestAsArray)
            {
                letters[character]--;

                //why it is -1 vs 0 condition?
                if (letters[character] < 0)
                {
                    Console.WriteLine($"character== {character}, letters[character] = {letters[character]} : (letters[character] < 0 ) = {letters[character] < 0}");
                    return(false);
                }
            }

            Console.WriteLine($" Is '{str1}' and {str2} are permutaion- True");
            Console.WriteLine("===================================================================================");

            return(true);
        }
Beispiel #5
0
 public void Run()
 {
     AssortedMethods.PrintType(type: typeof(StackAndQueue_Q3_01));
 }
Beispiel #6
0
    public static void QuestionsAndSolutionsByNumbers()
    {
        var dummy = new IQuestion[] {
            new Array_Q1_00(),
            new LinkedList_Q2_00(),
            new StackAndQueue_Q3_00(),
            new TreeAndGraph_Q4_00(),
            new BitManipulation_Q5_00(),
            new SortingAndSearching_Q10_00(),
        };
        var questionsSetByNumbers = new[]
        {
            //new IQuestion[] {
            //    new Array_Q1_01_Is_String_Has_Unique_Chars(),
            //    new LinkedList_Q2_01_Remove_Duplicates(),
            //    new StackAndQueue_Q3_01(),
            //    new TreeAndGraph_Q1_01_Route_Beetween_Nodes(),
            //    new BitManipulation_Q5_01(),
            //    new SortingAndSearching_Q10_01(),
            //},
            new IQuestion[] {
                //new Array_Q1_02_Check_For_Permutations(),
                //new LinkedList_Q2_02_Return_Kth_To_Last(),
                //new StackAndQueue_Q3_02_StackWithMinFunction(),
                //new TreeAndGraph_Q4_02_CreateMinimalTree(),
                //new BitManipulation_Q5_02_PrintBinaryToString(),
                //new SortingAndSearching_Q10_02_Group_Anagrams(),
            },
            // new IQuestion[] {
            //    new Array_Q1_03(),
            //    new LinkedList_Q2_03(),
            //    new StackAndQueue_Q3_03(),
            //    new TreeAndGraph_Q4_03(),
            //    new BitManipulation_Q5_03(),
            //    new SortingAndSearching_Q10_03(),
            //},
            // new IQuestion[] {
            //    new Array_Q1_04(),
            //    new LinkedList_Q2_04(),
            //    new StackAndQueue_Q3_04(),
            //    new TreeAndGraph_Q4_04(),
            //    new BitManipulation_Q5_04(),
            //    new SortingAndSearching_Q10_04(),
            //},
            // new IQuestion[] {
            //    new Array_Q1_05(),
            //    new LinkedList_Q2_05(),
            //    new StackAndQueue_Q3_05(),
            //    new TreeAndGraph_Q4_05(),
            //    new BitManipulation_Q5_05(),
            //    new SortingAndSearching_Q10_05(),
            //},
            // new IQuestion[] {
            //    new Array_Q1_06(),
            //    new LinkedList_Q2_06(),
            //    new StackAndQueue_Q3_06(),
            //    new TreeAndGraph_Q4_06(),
            //    new BitManipulation_Q5_06(),
            //    new SortingAndSearching_Q10_06(),
            //},
            // new IQuestion[] {
            //    new Array_Q1_07(),
            //    new LinkedList_Q2_07(),
            //    new TreeAndGraph_Q4_07(),
            //    new BitManipulation_Q5_07(),
            //    new SortingAndSearching_Q10_07(),
            //},
            // new IQuestion[] {
            //    new Array_Q1_08(),
            //    new LinkedList_Q2_08(),
            //    new TreeAndGraph_Q4_08(),
            //    new BitManipulation_Q5_08(),
            //    new SortingAndSearching_Q10_08(),
            //},
            // new IQuestion[] {
            //    new Array_Q1_09(),
            //    new TreeAndGraph_Q4_09(),
            //    new SortingAndSearching_Q10_09(),
            //},
            // new IQuestion[] {
            //    new TreeAndGraph_Q4_10(),
            //    new SortingAndSearching_Q10_10(),
            //},
            // new IQuestion[] {
            //    new TreeAndGraph_Q4_11(),
            //    new SortingAndSearching_Q10_11(),
            //},
            //  new IQuestion[] {
            //    new TreeAndGraph_Q4_12(),
            //},
        };

        foreach (var questionsSet in questionsSetByNumbers)
        {
            foreach (IQuestion q in questionsSet)
            {
                Console.WriteLine(string.Format("{0}{1}", Environment.NewLine, Environment.NewLine));
                AssortedMethods.PrintType(q);
                q.Run();
            }

            Console.WriteLine();
            Console.WriteLine("Press Enter to continue..");
            //Console.ReadLine();
        }
    }