Skip to content

Instantly share code, notes, and snippets.

@airekans
Last active December 20, 2015 16:59
Show Gist options
  • Save airekans/6165573 to your computer and use it in GitHub Desktop.
Save airekans/6165573 to your computer and use it in GitHub Desktop.
[OJ] Careercup Top 150(1.1): Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? Solution: use a character code hash table.
static int count[256];
bool unique(const std::string& s)
{
memset(count, 0, sizeof(count));
const int size = s.size();
for (int i = 0; i < size; ++i)
{
if (count[s[i]] > 0)
{
return false;
}
else
{
++count[s[i]];
}
}
return true;
}
def unique(s):
char_table = [False for _ in xrange(256)]
for c in s:
i = ord(c)
if char_table[i]:
return False
else:
char_table[i] = True
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment