-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInnerMost_Parenthesis.cpp
81 lines (65 loc) · 1.63 KB
/
InnerMost_Parenthesis.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Aruna as a type writer, typing a document in her laptop. Suddently her system got
// hacked and whatever she types as words are displaying in reverse and with simple
// braces. She types only lowercase letters.
// Inorder to get the actual words, Aruna has to reverse each word starting with the
// word which is in innermost most braces and remove those braces.
// Help Aruna to get actual words.
// Constraints:
// ------------
// - 0 <= word.length <= 2000
// - Word only contains lower case English characters and parentheses.
// It's guaranteed that all braces are balanced.
// Input Format:
// -------------
// Line-1: a string represents an encoded word.
// Output Format:
// --------------
// return the original string.
// Sample Input-1:
// ---------------
// (pqrs)
// Sample Output-1:
// ----------------
// srqp
// Sample Input-2:
// ---------------
// (uoy(are)woh)
// Sample Output-2:
// ----------------
// howareyou
// Explanation
// ------------
// Initially "are" will be revesed as "era".
// Then (uoyerawoh) will be reversed.
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
stack<char> st;
for(char i:s){
if(i=='('){
st.push('(');
}
else if(i==')'){
string x="";
while(st.top()!='('){
x=x+st.top();
st.pop();
}
st.pop();
for(char i:x){
st.push(i);
}
}
else{
st.push(i);
}
}
string res="";
while(!st.empty()){
res=st.top()+res;
st.pop();
}
cout<<res;
}