Last active
December 20, 2015 16:59
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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