-
Notifications
You must be signed in to change notification settings - Fork 0
/
removedup.cpp
49 lines (45 loc) · 1.02 KB
/
removedup.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
class Solution{
public:
string remove(string s){
// remove duplicate elements
int tail = 1;
// iterate through the string and append the unique characters
for(int i = 1;i<s.length();i++){
int j;
for(j = 0; j<tail;j++){
if(s[i] == s[j]) break;
}
if(j == tail)
s[tail++] = s[i];
}
s[tail] = 0;
return s;
}
};
string removeDups(string s){
int curr, last;
curr = 0;
last = 0;
sort(s.begin(), s.end());
for(int i = 0; i < s.size();i++){
if(s[i] != s[last]){
last++;
s[last] = s[i];
}
}
return s.substr(0,last+1);
}
int main(){
string st = "aabcbbddefeefg";
//cin>>st;
//auto s = Solution();
//string result = s.remove(st);
//cout<<result<<"\n";
string s2 = removeDups(st);
cout<<s2;
//sort(st, st + st.length());
return 0;
}