String Algorithm - Validating If String Has Unique Characters

Problem Statement 

 
Write a program to validate if the given string contains unique characters or not; which means that the given string should not contain any characters twice or multiple times. You need to consider that the comparison is case non-sensitive and the whitespace is significant.
 
Example 1
 
Input - s1 = "hemant”
Output - No Duplicate characters found in the string
Explanation - s1 does contain each character one time.
 
Example 2
 
Input - s1 = "hemantH"
Output - Duplicate characters found in the string
Explanation - s1 contains character h two times irrespective of case sensitive.
 
Example 3
 
Input - s1 = "hemant H"
Output - Duplicate characters found in the string
Explanation - s1 contains character h two times irrespective of case sensitive and white space is significant.
 

Solution

 
Look at the steps described below for the algorithm.
 

Step 1 - First, validate the length of the given string

 
As per the ASCII standard character set, there are 128 characters for electronic communication. If the length of the given string is more than 128 characters, then print the result “Duplicate characters found in the string” and exit the program; else, continue with step 2.
 
Please note that more than 128 characters in the given string indicate that the string contains one or more characters twice or multiple times
 

Step 2 - Convert the given string into an array of type character

 
Declare an array of integer type with the 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, increment the value in the integer array by 1 if the value is 0. Otherwise, print the result “Duplicate characters found in the string” and exit the program.
 

Complete code of the algorithm

Here, I have given the complete program for finding if a given string contains duplicate characters. The code is well-commented to make you understand what each line means. 
  1. namespace HasUniqueCharacters  
  2. {  
  3.     /// Write a program to validate if the given string contains unique characters which means that the give string does not contain any character twice or multiple times  
  4.     /// You need to consider that comparison is case non-sensitive and whitespace is significant  
  5.     /// ============================================================  
  6.     /// For example 1:  
  7.     /// Input: s1 = "hemant"  Output: No Duplicate characters found in string  
  8.     /// Explanation: s1 does contain each character one time.  
  9.     /// ============================================================  
  10.     /// For example 2: "hemantH";  
  11.     /// Input: s1 = "hemantH" Output: Duplicate characters found in string  
  12.     /// Explanation: s1 contains character h two times irrespective of case sensitive.  
  13.     ///   
  14.     /// For example 3: "hemant H";  
  15.     /// Input: s1 = "hemantH" Output: Duplicate characters found in string  
  16.     /// Explanation: s1 contains character h two times irrespective of case sensitive and white space is significant.  
  17.     class Program  
  18.     {  
  19.         static void Main(string[] args)  
  20.         {  
  21.             Console.Write("Please enter string to be processed: ");  
  22.             var inputString = Console.ReadLine();  
  23.   
  24.             if (!string.IsNullOrWhiteSpace(inputString))  
  25.             {  
  26.                 //as per the ASCII standard character set there are 128 characters for electronic communication  
  27.                 //so the length of the given string is more than 128 means string contain character multiple times  
  28.                 if (inputString.Length > 128)  
  29.                 {  
  30.                     Console.WriteLine("Duplicate characters found in string");  
  31.                 }  
  32.   
  33.                 bool hasUniqueCharacters = HasUniqueCharacters(inputString);  
  34.                 Console.WriteLine(hasUniqueCharacters ? "No Duplicate characters found in string" : "Duplicate characters found in string");  
  35.             }  
  36.   
  37.             Console.ReadLine();  
  38.         }  
  39.   
  40.         private static bool HasUniqueCharacters(string inputString)  
  41.         {  
  42.             string stringToBeProcessed = inputString.Replace(" """).ToLower();  
  43.             //Convert string into an array f characters   
  44.             char[] chars = stringToBeProcessed.ToCharArray();  
  45.             //declare an array of integer type with size 128 because as per the ASCII standard character set  
  46.             //there are 128 characters for electronic communication.  
  47.             int[] characterSetMappingTable = new int[128];  
  48.   
  49.             //Iterate through an array and for the index matching with the character ASCII value,  
  50.             //increment the value in the integer array by 1 if the value is 0 otherwise return false if the value is already 1   
  51.             foreach (char c in chars)  
  52.             {  
  53.                 var characterIndex = (int)c;  
  54.                 if (characterSetMappingTable[characterIndex] >= 1)  
  55.                     return false;  
  56.                 characterSetMappingTable[characterIndex]++;  
  57.             }  
  58.   
  59.             return true;  
  60.         }  
  61.     }  
  62. }     

Time Complexity

 
O(n) - Let me explain how this is calculated. The 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 think can be followed to make it better and more optimized.


Similar Articles