String Algorithm - Program To Validate Given String A Palindrome Permutation

Problem Statement

 
Write a program to check if the given string is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backward. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words. You need to consider that comparison is case insensitive, whitespace is non-significant and all the non-letter characters.
 
Example 1
 
Input: s1 = "mama d"
 
Output: Palindrome Permutation (permutation: madam)
 
Explanation: there are 5 non-letter characters in the given string "mama d" and this is a permutation of a palindrome because it has two M, two A, one d. Strings of an odd length must have exactly one character with an odd count.
 
Example 2
 
Input: s1 = "Tic Toe"
 
Output: No Palindrome Permutation
 
Explanation: there are 6 non-letter characters in the given string "Tic Toe" and this is not a permutation of a palindrome because it has two T, One C, One O One E, One I (more than one character with odd count)
 

Solution

 
Look at the steps described for an algorithm
  • Convert the given string 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 an 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 otherwise set the value in the integer array to 0 if it is already 1

Check that no more than one character has an odd count in the integer character set. If more than one characters with odd count is found, then return failure signal and print the result “No Palindrome Permutation” otherwise “Palindrome Permutation”

  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             Console.Write("Please enter string to be processed: ");  
  6.             var inputString = Console.ReadLine();  
  7.             var characterSetMappingTable = PrepareCharacterSetMappingTable(inputString);  
  8.             bool isOnlyOddOneFound = IsOnlyOddOneFound(characterSetMappingTable);  
  9.             Console.WriteLine(isOnlyOddOneFound ? "Palindrome Permutation" : "No Palindrome Permutation");  
  10.   
  11.             Console.ReadLine();  
  12.         }  
  13.   
  14.         private static int[] PrepareCharacterSetMappingTable(string inputString)  
  15.         {  
  16.             string stringToBeProcessed = inputString.Replace(" """).ToLower();  
  17.             //Convert string into an array f characters   
  18.             char[] chars = stringToBeProcessed.ToCharArray();  
  19.             //declare an array of integer type with size 128 because as per the ASCII standard character set  
  20.             //there are 128 characters for electronic communication.  
  21.             int[] characterSetMappingTable = new int[128];  
  22.   
  23.             //Iterate through an array and for the index matching with the character ASCII value,  
  24.             //set the value in the integer array to 1 if the value is 0 otherwise set the value  in the integer array to 0 if it is already 1   
  25.             foreach (char c in chars)  
  26.             {  
  27.                 var characterIndex = (int)c;  
  28.   
  29.                 if (characterSetMappingTable[characterIndex] == 0)  
  30.                     characterSetMappingTable[characterIndex] = 1;  
  31.                 else if (characterSetMappingTable[characterIndex] == 1)  
  32.                     characterSetMappingTable[characterIndex] = 0;  
  33.             }  
  34.   
  35.             return characterSetMappingTable;  
  36.         }  
  37.   
  38.         /// <summary>  
  39.         /// this method will determine that no more than one character has an odd count.  
  40.         /// </summary>  
  41.         /// <param name="characterSetMappingTable"></param>  
  42.         /// <returns></returns>  
  43.         private static bool IsOnlyOddOneFound(int[] characterSetMappingTable)  
  44.         {  
  45.             int countOddOneFound = 0;  
  46.   
  47.             foreach (var index in characterSetMappingTable)  
  48.             {  
  49.                 if (countOddOneFound > 1)  
  50.                     return false;  
  51.                 if (index == 1)  
  52.                 {  
  53.                     countOddOneFound++;  
  54.                 }  
  55.             }  
  56.   
  57.             return true;  
  58.         }  
  59.     }  
Time Complexity
 
O(n )
 
array iteration takes O(n) time to iterate through n characters where n is the length of the string.
 
Note
Please feel free to propose or suggest a solution you might think can be followed to make it better and more optimized.


Similar Articles