Explained With Images. Commented Solution.
Approach\nIn this solution, we will use a stack with its method, LIFO, to remove all the extra parentheses from the input string. We traverse the input string, 
# Approach\nIn this solution, we will use a stack with its method, LIFO, to remove all the extra parentheses from the input string. We traverse the input string, and every time we encounter an opening parenthesis, we push it, along with its index, onto the stack and keep traversing. Meanwhile, whenever we find a closing parenthesis, we decide whether to push it onto the stack or not.\n\n![Screenshot 2024-08-12 at 5.06.41\u202FPM.png](https://assets.leetcode.com/users/images/c5feaac9-5a96-47a1-b55a-eeee6d7472db_1723496867.501126.png)\n![Screenshot 2024-08-12 at 5.06.44\u202FPM.png](https://assets.leetcode.com/users/images/b7414cb7-6dbf-432d-8c02-6d3262c523cb_1723496870.45521.png)\n![Screenshot 2024-08-12 at 5.06.49\u202FPM.png](https://assets.leetcode.com/users/images/9229656f-db40-472e-aa90-e90d09c50a8e_1723496873.748117.png)\n![Screenshot 2024-08-12 at 5.06.52\u202FPM.png](https://assets.leetcode.com/users/images/f80e0b1e-1962-4bb7-a682-465320a2fa96_1723496876.5600693.png)\n![Screenshot 2024-08-12 at 5.06.53\u202FPM.png](https://assets.leetcode.com/users/images/295c1b87-e4b2-4de5-8651-2f6abf922c5f_1723496879.0078664.png)\n![Screenshot 2024-08-12 at 5.06.55\u202FPM.png](https://assets.leetcode.com/users/images/f4ad1108-ff50-4225-b5b8-207582b35a15_1723496881.4688091.png)\n![Screenshot 2024-08-12 at 5.06.57\u202FPM.png](https://assets.leetcode.com/users/images/7ab23116-6f40-4dc8-87d7-afaaff3f0828_1723496884.5131025.png)\n![Screenshot 2024-08-12 at 5.06.58\u202FPM.png](https://assets.leetcode.com/users/images/35c57936-612a-4471-a257-679a6b69cca5_1723496885.9868083.png)\n![Screenshot 2024-08-12 at 5.07.00\u202FPM.png](https://assets.leetcode.com/users/images/f0496b93-885c-4cc1-ab17-8787735406f1_1723496888.0541625.png)\n![Screenshot 2024-08-12 at 5.07.01\u202FPM.png](https://assets.leetcode.com/users/images/bf4d9c9e-a816-400c-a615-83f3a4720f15_1723496890.267824.png)\n![Screenshot 2024-08-12 at 5.07.03\u202FPM.png](https://assets.leetcode.com/users/images/b45cd96e-c42e-40ec-be68-6c23b0d1b291_1723496892.1820626.png)\n![Screenshot 2024-08-12 at 5.07.04\u202FPM.png](https://assets.leetcode.com/users/images/3b55781e-9bdc-4552-bf47-5cbbf5692338_1723496894.2552106.png)\n![Screenshot 2024-08-12 at 5.07.07\u202FPM.png](https://assets.leetcode.com/users/images/94a019d5-0ada-4f92-9a90-e7c8cd391d11_1723496896.132623.png)\n\n\n# Time Complexity $$O(N)$$\n\n# Space Complexity $$O(N)$$\n\n# Code\n```\nclass Solution {\n    public String minRemoveToMakeValid(String s) {\n\n       Stack<int[]> stack = new Stack<>();\n        char[] sArray = s.toCharArray();\n\n        for (int i = 0; i < s.length(); i++) {\n            char val = s.charAt(i);\n            // if stack is not empty and top element of stack is an opening parenthesis\n            // and the current element is a closing parenthesis\n            if (!stack.isEmpty() && stack.peek()[0] == \'(\' && val == \')\') {\n                // pop the opening parenthesis as it makes a valid pair \n                // with the current closing parenthesis\n                stack.pop();\n            }\n            // if the current value is an opening or a closing parenthesis \n            else if (val == \'(\' || val == \')\') {\n                // push onto stack\n                stack.push(new int[]{val, i});\n            }\n        }\n\n        // Remove the invalid parentheses\n        while (!stack.isEmpty()) {\n            sArray[stack.pop()[1]] = \' \';\n        }\n\n        // Convert the char array to a string\n        StringBuilder result = new StringBuilder();\n        for (char c : sArray) {\n            if (c != \' \') {\n                result.append(c);\n            }\n        }\n\n        return result.toString();\n    }\n}\n```\n\n# A single upvote is all it takes :)\nIf found helpful, give a upvote. Keeps me motivated to post more solutions. Thanks!\n\n\n\n\n\n\n
['String', 'Stack', 'C++', 'Java', 'Python3']