-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSuperReducedString.cpp
More file actions
51 lines (39 loc) · 1.11 KB
/
SuperReducedString.cpp
File metadata and controls
51 lines (39 loc) · 1.11 KB
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
#include <bits/stdc++.h>
using namespace std;
/*
* Complete the 'superReducedString' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/
string superReducedString(string s) {
// Using a stack to perform the reduction
stack<char> st;
for (char ch : s) {
if (!st.empty() && st.top() == ch) {
// If the top of the stack is the same as the current character, pop the stack
st.pop();
} else {
// Otherwise, push the current character onto the stack
st.push(ch);
}
}
// Reconstruct the reduced string from the stack
string result = "";
while (!st.empty()) {
result = st.top() + result; // Add characters in reverse order
st.pop();
}
// If the resultant string is empty, return "Empty String"
return result.empty() ? "Empty String" : result;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string s;
getline(cin, s);
string result = superReducedString(s);
fout << result << "\n";
fout.close();
return 0;
}