Problem Statement
Write a program to validate if two given strings are a permutation-combination of each other. You need to consider that the comparison is case sensitive and whitespace is significant
Example 1
Input - s1 = "bat" s2 = "tab"
Output - True
Explanation - s2 is a permutation of s1("bat").
Example 2
Input - s1 = "God" s2 = "doG"
Output - True
Explanation - s2 is a permutation of s1("God").
Example 3
Input - s1 = "bat" s2 = "Tab"
Output - True
Explanation - s2 is not a permutation of s1("bat") because s2 contains ‘T’ in lowercase whereas s1 contains ‘t’ in lowercase.
Example 4
Input - s1 = "abc" s2 = "abcd"
Output - True
Explanation - s2 is not a permutation of s1("abcd") because s2 contains 4 characters whereas s1 contains 3 characters.
Solution
As per the definition of permutation, we know they have the same characters but in different orders. So, let’s have a look at the steps described for an algorithm.
Compare the length of both the strings. If they do not match, then it concludes that the given strings are not in a permutation. Print the result and exit the program; else continue with the following steps.
- Convert both the given strings into an array of type character.
- Declare an array of integer type with size 128 that maps the ASCII value because as per the ASCII standard character set, there are 128 characters for electronic communication.
- Iterate through the first array and for the index matching with the character ASCII value, set the value in the integer array to 1 if the value is 0 OR set the value in the integer array to 0 if the value is 1.
- Iterate through the second array and for the index matching with the character ASCII value, set the value in the integer array to 1 if the value is 0 OR set the value in the integer array to 0 if the value is 1
After iterating over both the arrays, the resultant value in the integer for each of the indices should be 0. In case we see any index with odd value, i.e., 1, then it concludes that the given strings are not permutations.
- class Program
- {
- static void Main(string[] args)
- {
- Console.Write("Please enter first string to be processed: ");
- var firstInputString = Console.ReadLine();
-
- Console.Write("Please enter second string to be processed: ");
- var secondInputString = Console.ReadLine();
-
- if (!string.IsNullOrWhiteSpace(firstInputString) && !string.IsNullOrWhiteSpace(secondInputString))
- {
-
- Console.WriteLine(IsPermutation(firstInputString, secondInputString) ? "Given strings are permutation of each other"
- : "Given strings are not permutation of each other");
- }
-
- Console.ReadLine();
- }
-
- private static bool IsPermutation(string firstInputString, string secondInputString)
- {
-
- if (firstInputString.Length != secondInputString.Length)
- {
- return false;
- }
-
-
- char[] firstInputStringCharacterSet = firstInputString.ToCharArray();
- char[] secondInputStringCharacterSet = secondInputString.ToCharArray();
-
-
- int[] characterSetMappingTable = new int[128];
-
-
-
- PrepareCharacterSetMappingTable(firstInputStringCharacterSet, characterSetMappingTable);
-
-
- PrepareCharacterSetMappingTable(secondInputStringCharacterSet, characterSetMappingTable);
-
-
-
-
- return IsOddOneFound(characterSetMappingTable);
- }
-
- private static void PrepareCharacterSetMappingTable(char[] inputStringCharacterSet, int[] characterSetMappingTable)
- {
- foreach (char inputStringCharacter in inputStringCharacterSet)
- {
- var characterIndex = (int)inputStringCharacter;
-
- switch (characterSetMappingTable[characterIndex])
- {
- case 0:
- characterSetMappingTable[characterIndex] = 1;
- break;
- case 1:
- characterSetMappingTable[characterIndex] = 0;
- break;
- }
- }
- }
-
-
-
-
-
-
- private static bool IsOddOneFound(int[] characterSetMappingTable)
- {
- return characterSetMappingTable.All(index => index != 1);
- }
- }
Time Complexity
O(n + n + 128) which is same as O(n).
Let me explain how this is calculated. The first array iteration takes O(n) time to iterate through n characters where n is the length of the string. Similarly, the second array iteration also takes O(n) time to iterate through n characters where n is the length of the string. Finally, iterate through the results to validate if any index contains an odd value in the integer array takes O(128) – a constant time.
Please feel free to propose or suggest a solution you think can be followed to make it better and more optimized.