Last active
December 13, 2015 21:09
-
-
Save JasonGitHub/4975301 to your computer and use it in GitHub Desktop.
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
| // Time: n Space: n | |
| bool HasUniqueChars(char* str, int n) { | |
| bitset<256> s; | |
| for (size_t i = 0; i < n; ++i) { | |
| int val = str[i]; | |
| if (s[val]) return false; | |
| else s[val] = true; | |
| } | |
| return true; | |
| } | |
| // assume class string to use STL sort() | |
| // Time: n * log (n) Space: 0 | |
| bool HasUniqueChars1(string s) { | |
| sort(s.begin(), s.end()); | |
| for (size_t i = 1; i < s.size(); ++i) { | |
| if (s[i] == s[i - 1]) return false; | |
| } | |
| return true; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment