-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathpotd_24_03_2023.cpp
53 lines (51 loc) · 1.13 KB
/
potd_24_03_2023.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
50
51
52
53
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
string removeReverse(string s) {
unordered_map<char,int>m;
unordered_set<char>st;
int n=s.size();
for(int i=0;i<n;i++){
m[s[i]]++;
}
for(int i=0;i<n;i++){
st.insert(s[i]);
}
int dir=0;
int i=0;
int e=n-1;
while(s.size()>st.size()){
if(dir==0){
if(m[s[i]]<=1){
i++;
}
else{
m[s[i]]--;
s.erase(s.begin()+i);
e=s.size()-1;
dir=1;
}
}
else{
if(m[s[e]]<=1){
e--;
}
else{
m[s[e]]--;
s.erase(s.begin()+e);
i=0;
dir=0;
}
}
}
string d=s;
reverse(d.begin(),d.end());
if(dir==0){
return s;
}
else{
return d;
}
}
};