Advertisement
Catachan

UniqueCharSolution

Oct 25th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. bool unique(string input)
  2. {
  3.     char max = 0;
  4.    
  5.     foreach(char a in input)                                                    /* Find the value of the largest character in the input string (Complexity of N) */
  6.     {
  7.         if(a > max)
  8.             max = a;
  9.     }
  10. /* Generate a bool array that has a length long enough to store every character code up to the max found in the input string */
  11.     new bool Char_Used[(int) max] = FALSE; 
  12.  
  13. /* Iterate through the string. This loop will have a complexity of N */
  14.     foreach(char c in input)       
  15.     {
  16.     /* Use the code for each character to jump to that index in the array (complexity of a constant) */
  17.         if(Char_Used[c])                                                           
  18.             return FALSE;   /* If that index has a value of TRUE, then return false because the character is not unique. */
  19.            
  20.         else
  21.             Char_Used[c] = TRUE;    /* If the index is not already set to TRUE, this is the first appearance of that character so set that index to TRUE in case we see it again */
  22.     }
  23.    
  24.     return TRUE;                                                                    /* If we get to here, then there are no repeats in the entire string. */
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement