Skip to content

Commit df14ba7

Browse files
authored
Create TheRecusriveStackProblem.cpp
1 parent 353f7f6 commit df14ba7

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
vector<int> en;
4+
int go(int lo, int hi) {
5+
if(lo+1 == hi) return 1;
6+
int mid = en[lo];
7+
if(mid == hi) return 2*go(lo+1, hi-1);
8+
return go(lo, mid) + go(mid+1, hi);
9+
}
10+
11+
//calculates the score of string S
12+
//This is solution of my YouTube video "The Recursive Stack Problem on Strings"
13+
int scoreOfParentheses(string &S) {
14+
int i, n = (int)S.size();
15+
n = max(n, 1);
16+
en.resize(n, 0);
17+
stack<int> s;
18+
for(i=0; i<n; i++){
19+
if(S[i] == ')'){
20+
int t = s.top();
21+
en[t] = i;
22+
s.pop();
23+
}
24+
else s.push(i);
25+
}
26+
return go(0, n-1);
27+
}
28+
};

0 commit comments

Comments
 (0)