#include <bits/stdc++.h> 
bool checkPalindrome(string s)
{

    string result;
    for(char c : s){
       if (c >= 'A' && c <= 'Z') {
            result += c + 32;
        }else if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
            result = result + c;
        }
        else{
            continue;
        }
    }
     string rev = result;
     reverse(rev.begin(), rev.end());
     if(result == rev){
         return true;
     } else {
         return false;
     }

}

class Solution {
public:
    string removeOuterParentheses(string s) {
        string result;
        stack<char> stack;
        int start = 0;  // Start index of a primitive block
        int n = s.size();
        for(int i = 0; i <n; i++){
            if(s[i] == '(') {
                stack.push('(');
            }else{
                stack.pop();
            }
            // When the stack is empty, we found a complete primitive block
            if(stack.empty()){
                // Append all except the outermost parentheses
                result += s.substr(start + 1, i - start - 1);
                start = i + 1;  // Update the start for the next primitive block
            }
        }

        return result;
    }
};