Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool unique(string input)
- {
- char max = 0;
- foreach(char a in input) /* Find the value of the largest character in the input string (Complexity of N) */
- {
- if(a > max)
- max = a;
- }
- /* Generate a bool array that has a length long enough to store every character code up to the max found in the input string */
- new bool Char_Used[(int) max] = FALSE;
- /* Iterate through the string. This loop will have a complexity of N */
- foreach(char c in input)
- {
- /* Use the code for each character to jump to that index in the array (complexity of a constant) */
- if(Char_Used[c])
- return FALSE; /* If that index has a value of TRUE, then return false because the character is not unique. */
- else
- 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 */
- }
- return TRUE; /* If we get to here, then there are no repeats in the entire string. */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement