Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- // Time Complexity:- O(N)
- // Space Complexity:- O(N)
- int scoreOfParentheses(string s) {
- stack<int> st;
- for(auto& c:s){
- if(c=='('){
- st.push(-1);
- }
- else{
- int val = 0;
- while(st.top()!=-1){
- val += st.top();
- st.pop();
- }
- st.pop();
- if(val){
- st.push(val*2);
- }
- else{
- st.push(1);
- }
- }
- }
- int ans = 0;
- while(!st.empty() and st.top()!=-1){
- ans += st.top();
- st.pop();
- }
- return ans;
- }
- };
Add Comment
Please, Sign In to add comment