Last active
August 29, 2015 14:02
-
-
Save honghaoz/eae621c43197c72d4c60 to your computer and use it in GitHub Desktop.
Check whether characters are unique in a string
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
#inlcude <cstring> | |
#include <iostream> | |
#include <set> | |
#include <cstring> | |
using namespace std; | |
bool checkUniqueChars(char *str) { | |
char *strP = str; | |
set<char> testSet; | |
pair<set<char>::iterator, bool> result; | |
while (*(strP) != 0) { | |
result = testSet.insert(*(strP)); | |
if (result.second == false) { | |
return false; | |
} | |
strP++; | |
} | |
return true; | |
} | |
bool checkUniqueString(string s) { | |
set<char> testSet; | |
pair<set<char>::iterator, bool> result; | |
for (string::iterator it = s.begin(); it != s.end(); it++) { | |
result = testSet.insert(*it); | |
if (result.second == false) { | |
return false; | |
} | |
} | |
return true; | |
} | |
int main() { | |
char a[] = "12345abc"; | |
printf("%d\n", checkUniqueChars(a)); | |
char b[] = "12345abc123"; | |
printf("%d\n", checkUniqueChars(b)); | |
string aa = "12345abc"; | |
printf("%d\n", checkUniqueString(aa)); | |
string bb = "123452"; | |
printf("%d\n", checkUniqueString(bb)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment