Created
December 21, 2012 18:01
-
-
Save brooksbp/4354565 to your computer and use it in GitHub Desktop.
This file contains 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
// Implement an algorithm to determine if a string has all unique characters. | |
// What if you cannot use additional data structures? | |
#include <iostream> | |
#include <string> | |
#include <assert.h> | |
#include <unordered_map> | |
using namespace std; | |
bool all_unique(string &str) { | |
return true; | |
} | |
bool all_unique2(string &str) { | |
string::iterator i, j; | |
for (i = str.begin(); i < (str.end() - 1); i++) | |
for (j = i + 1; j < str.end(); j++) | |
if (*i == *j) return false; | |
return true; | |
} | |
int main(int argc, char *argv[]) { | |
string s1("a uniq-str"); | |
string s2("another"); | |
string empty(""); | |
string sc = s1 + s2 + empty; | |
assert(all_unique(s1)); | |
assert(all_unique(s2)); | |
assert(all_unique(empty)); | |
assert(!all_unique(sc)); | |
assert(all_unique2(s1)); | |
assert(all_unique2(s2)); | |
assert(all_unique2(empty)); | |
assert(!all_unique2(sc)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment