Skip to content

Instantly share code, notes, and snippets.

@honghaoz
Last active August 29, 2015 14:02
Show Gist options
  • Save honghaoz/eae621c43197c72d4c60 to your computer and use it in GitHub Desktop.
Save honghaoz/eae621c43197c72d4c60 to your computer and use it in GitHub Desktop.
Check whether characters are unique in a string
#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