-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathsuperReducedString.cpp
More file actions
71 lines (52 loc) · 1.61 KB
/
superReducedString.cpp
File metadata and controls
71 lines (52 loc) · 1.61 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <stack>
#include <deque>
#include <string>
// super reduced string hackerRank
// Reduce a string of lowercase characters in
// range ascii[‘a’..’z’]by doing a series of operations. In each operation, select a pair of adjacent letters that match, and delete them.
// aab shortens to b in one operation: remove the adjacent a characters.
// abccbd => ad
using namespace std;
// custom char inherited stack to use protected member c
class customCharStack : public std::stack<char, std::deque<char>>
{
public:
const std::deque<char>& get_underlying_container() const
{
return this->c; // c is protected in std::stack
}
};
string superReducedString(string str) {
customCharStack stackStr;
for(const auto s : str)
{
if(!stackStr.empty() && s == stackStr.top())
stackStr.pop();
else
stackStr.push(s);
}
const std::deque<char>& underlying_container = stackStr.get_underlying_container();
std::string result;
for(auto it = underlying_container.rbegin(); it != underlying_container.rend(); ++it)
{
result += *it;
}
if(result.empty())
return "Empty String";
return result;
}
int main() {
std::string str("acdqglrfkqyuqfjkxyqvnrtysfrzrmzlygfveulqfpdbhlqdqrrqdqlhbdpfqluevfgylzmrzrfsytrnvqyxkjfquyqkfrlacdqj");
const auto result = superReducedString(str);
std::cout<<result<<std::endl;
if(result != "acdqgacdqj")
{
std::cout<<"Output correct !";
}
else
{
std::cout<<"Output is not correct";
}
return 0;
}