Skip to content

Commit 8f06d05

Browse files
authored
Create 1957. Delete Characters to Make Fancy String (#623)
2 parents f3a6642 + 80ed826 commit 8f06d05

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
string makeFancyString(string s) {
4+
stack<char>st;
5+
for(int i = 0;i<s.length();i++){
6+
st.push(s[i]);
7+
if(st.size()>=3){
8+
char first = st.top();
9+
st.pop();
10+
char second = st.top();
11+
st.pop();
12+
char third = st.top();
13+
st.pop();
14+
if(first == second && second == third){
15+
16+
st.push(second);
17+
st.push(first);
18+
}else{
19+
20+
st.push(third);
21+
st.push(second);
22+
st.push(first);
23+
}
24+
}
25+
}
26+
27+
string ans = "";
28+
while(!st.empty()){
29+
ans += st.top();
30+
st.pop();
31+
32+
}
33+
reverse(ans.begin(),ans.end());
34+
return ans;
35+
}
36+
};

0 commit comments

Comments
 (0)