-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdele_duplicatestring.cpp
44 lines (37 loc) · 1.01 KB
/
dele_duplicatestring.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
#include<bits/stdc++.h>
using namespace std;
string removeDuplicateLetters(string s) {
unordered_map<char,int>map;
vector<bool>visted(26, 0);
stack<char>st;
for(auto it: s){
map[it]++;
}
for(auto it : s){
if(visted[it - 'a']){
map[it]--;
continue;
}
while(!st.empty() && st.top() > it && map[st.top()] > 0){
visted[st.top() - 'a'] = 0;//出栈,没有访问过
cout<<st.top()<<' '<<visted[it - 'a']<<endl;
st.pop();
}
st.push(it);
cout<<it<<endl;
visted[it - 'a'] = 1;
map[it]--;
}
string ret;
while(!st.empty()){
char ch = st.top();
st.pop();
ret.push_back(ch);
}
return ret;
}
int main(){
string s = "bcabc";
string ret = removeDuplicateLetters(s);
cout<<ret<<endl;
}