Exemple #1
0
        private static bool NoSortImplementation(StringContainer container)
        {
            /*
             * Permutations contains the same symbols exactly the same amount of time in both strings
             * Simple count and check
             */

            // Space complexity O(n)
            // Time complexity O(2 n)

            // Say ASCII or HashSet for Unicode :)
            var alphabet = new int[128];

            for (var i = 0; i < container.First.Length; i++)
            {
                alphabet[container.First[i]]++;
            }

            for (var i = 0; i < container.Second.Length; i++)
            {
                alphabet[container.Second[i]]--;

                if (alphabet[container.Second[i]] < 0)
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #2
0
        private static bool NaiveImplementation(StringContainer container)
        {
            // Space compexity O(1)
            // Time complexity two sorts assuming quick sort log(n) O(2 log(n))

            var left  = container.First.ToCharArray();
            var right = container.Second.ToCharArray();

            Array.Sort(left);
            Array.Sort(right);

            return(left.SequenceEqual(right));
        }