Skip to content

Instantly share code, notes, and snippets.

@sebastibe
Created January 5, 2013 02:32
Show Gist options
  • Save sebastibe/4459315 to your computer and use it in GitHub Desktop.
Save sebastibe/4459315 to your computer and use it in GitHub Desktop.
// Assuming we are working with the ASCII character set. 256 unique values.
//
// First, check that the length of the string is under the number of unique values.
// If it is greater, the string does not have all unique characters
//
// Second, create an array of boolean values of size of unique characters.
// Traverse the string and flag the array value from false to true when a character is hit.
// When the character is hit a second time, return false.
bool hasUniqueChars(char * string)
{
if (strlen(string) > 256) // Step One
return false;
int length = strlen(string);
bool * char_set = new bool[256];
for (int i = 0; i < length; ++i) // Step Two
{
int val = string[i];
if (char_set[val] == true) // If the character was already hit
{
delete char_set;
return false;
}
char_set[val] = true;
}
delete char_set;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment