Skip to content

Instantly share code, notes, and snippets.

@Se7soz
Last active January 3, 2016 05:09
Show Gist options
  • Select an option

  • Save Se7soz/8414163 to your computer and use it in GitHub Desktop.

Select an option

Save Se7soz/8414163 to your computer and use it in GitHub Desktop.
Read the How to prepare for an interview series at my blog: http://se7so.blogspot.com/2014/01/how-to-prepare-for-interview.html
#include <iostream>
#include <cstring>
using namespace std;
string removeDuplicates(string s) {
bool chs[512];
memset(chs, false, sizeof chs);
int j = 0;
for(int i = 0; i < s.size(); i++) {
if(!chs[s[i]]) {
s[j++] = s[i];
chs[s[i]] = true;
}
}
return s.substr(0, j);
}
int main() {
string s = "aaaabaaaacbbbbd";
s = removeDuplicates(s);
cout << s << endl;
cout << removeDuplicates("a") << endl;
cout << removeDuplicates("") << endl;
cout << removeDuplicates("aaaaaaaaaaaaaaa") << endl;
cout << removeDuplicates("aaaaaaaaaabbbbbbbbbbbbcccccccccc") << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment