-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
205 lines (188 loc) · 3.34 KB
/
Copy pathstack.cpp
File metadata and controls
205 lines (188 loc) · 3.34 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "stc++.h"
using namespace std;
/*******************/
/* Stack functions */
/*******************/
void printStack(stack<int> myStack) {
cout << "Stack size: " << myStack.size() << endl;
while (!myStack.empty()) {
cout << myStack.top() << endl;
myStack.pop();
}
}
/* 1047. Remove All Adjacent Duplicates In String */
int minAddToMakeValid(string S) {
stack<char> parentheses;
int carry = 0;
int res = 0;
for (int i = 0; i < S.length(); i++) {
parentheses.push(S[i]);
}
while (!parentheses.empty()) {
if (parentheses.top() == '(') {
if (!carry)
res++;
else
carry--;
}
else {
carry++;
}
parentheses.pop();
}
res += carry;
return res;
}
/* 1047. Remove All Adjacent Duplicates In String */
string removeDuplicates(string S) {
stack<char> tmp;
if (S.length() == 0)
return S;
tmp.push(S[0]);
for (int i = 1; i < S.length(); i++) {
if (tmp.empty() || S[i] != tmp.top())
tmp.push(S[i]);
else
tmp.pop();
}
string res = "";
while (!tmp.empty()) {
res = tmp.top() + res;
tmp.pop();
}
return res;
}
/* 856. Score of Parentheses */
int scoreOfParentheses(string S) {
int res = 0;
int t_res = 0;
int prev = 0;
int curr = 0;
int flag = 1;
int rightP = 0;
stack<char> tmp;
for (int i = 0; i < S.length(); i++) {
tmp.push(S[i]);
}
while (!tmp.empty()) {
if (tmp.top() == ')') {
res += t_res;
t_res = 0;
rightP++;
curr = 0;
}
else {
rightP--;
if (curr == 0) {
prev = 0;
curr = 1;
t_res += 1;
}
else {
prev = 1;
t_res *= 2;
}
}
tmp.pop();
if (rightP == 0 && !tmp.empty()) {
flag = 0;
}
}
if (flag)
res = (res + (t_res / 2)) * 2;
else
res += t_res;
return res;
}
/* 844. Backspace String Compare */
bool backspaceCompare(string S, string T) {
stack<char> chr_s;
stack<char> chr_t;
for (auto s : S) {
if (s == '#') {
if (!chr_s.empty())
chr_s.pop();
}
else {
chr_s.push(s);
}
}
for (auto t : T) {
if (t == '#') {
if (!chr_t.empty())
chr_t.pop();
}
else {
chr_t.push(t);
}
}
while (!chr_s.empty() and !chr_t.empty()) {
if (chr_s.top() != chr_t.top())
return false;
chr_s.pop();
chr_t.pop();
}
if (!chr_s.empty() or !chr_t.empty())
return false;
return true;
}
/* 20. Valid Parentheses */
bool isValid(string s) {
stack<char> left;
stack<char> right;
for (auto i : s) {
left.push(i);
}
while (!left.empty()) {
if (left.top() == '(') {
if (right.empty()) {
return false;
}
else {
if (right.top() == ')') {
left.pop();
right.pop();
}
else {
return false;
}
}
}
else if (left.top() == '[') {
if (right.empty()) {
return false;
}
else {
if (right.top() == ']') {
left.pop();
right.pop();
}
else {
return false;
}
}
}
else if (left.top() == '{') {
if (right.empty()) {
return false;
}
else {
if (right.top() == '}') {
left.pop();
right.pop();
}
else {
return false;
}
}
}
else {
right.push(left.top());
left.pop();
}
}
if (!right.empty())
return false;
return true;
}
/* 1190. Reverse Substrings Between Each Pair of Parentheses */