👏Beats 96.03% of users with Java || ✅Simple & Easy Without Space Well Explained Solution 🔥💥
Intuition\ncount variable is counting parentheses and we make changes in stringbuilder sb.\nKeep counting the opening parentheses and when the closing parenthes
# Intuition\ncount variable is counting parentheses and we make changes in stringbuilder sb.\nKeep counting the opening parentheses and when the closing parentheses comes, ignore it and decrement the count.\nThen if count is greater than 0, then remove as many opening parentheses from the back as count.\n\n# I Think This Can Help You(For Proof Click on the Image)\n[![Screenshot 2024-02-24 232407.png](https://assets.leetcode.com/users/images/830962fd-8379-41dc-b6e4-2380434fec04_1712385657.5529292.png)](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/submissions/1224581986/?envType=daily-question&envId=2024-04-06)\n\n# Approach\n1. Initilize string builder sb, because string is immutable we can not change anything in the string.\n    ```\n    StringBuilder sb = new StringBuilder(s);\n    ```\n2. Initilize count variable to count parentheses.\n    ```\n    int count = 0;\n    ```\n3. Iterate the loop from 0 to sb\'s length.(Below code in the loop)\n    - Check if ith charcter is opening parentheses then increase count by 1.\n        ```\n        if(sb.charAt(i)==\'(\'){\n            count++;\n        }\n        ```\n    - Check if ith character is closing parentheses and count is more than 0 then decrease count by 1.\n        ```\n        else if(sb.charAt(i) == \')\' && count>0){\n            count--;\n        }\n        ```\n    - Check if ith character is closing parentheses and count will be 0 then delete ith character and i decreased by 1, because one character will be removed from the string builder, so its length will be reduced by one and a new index will be created in which the middle character will not be forgotten, so we have to decrease i.\\\n        ```\n        else if(sb.charAt(i) == \')\' && count==0){\n            sb.delete(i,i+1);\n            i--;\n        }\n        ```\n- Completed loop look like this\n    ```\n    for(int i=0;i<sb.length();i++){\n        if(sb.charAt(i)==\'(\'){\n            count++;\n        } else if(sb.charAt(i) == \')\' && count>0){\n            count--;\n        } else if(sb.charAt(i) == \')\' && count==0){\n            sb.delete(i,i+1);\n            i--;\n        }\n    }\n    ```\n4. After that if we have extra opening parentheses we remove extra opening parentheses from back of the string builder.\n    ```\n    for(int i=sb.length()-1;i>=0;i--){\n        if(count==0){\n            break;\n        }\n        if(sb.charAt(i)==\'(\'){\n            sb.delete(i,i+1);\n            count--;\n        }\n    }\n    ```\n5. our return type is string so we convert string builder to string and return it.\n    ```\n    return sb.toString();\n    ```\n\n# Complexity\n- Time complexity:\nO(n)   --> We go linearlly.\n\n- Space complexity:\nO(1)   --> we do not uise any extra space.\n\n# Code\n```\nclass Solution {\n    public String minRemoveToMakeValid(String s) {\n        StringBuilder sb = new StringBuilder(s);\n        int count = 0;\n        for(int i=0;i<sb.length();i++){\n            if(sb.charAt(i)==\'(\'){\n                count++;\n            } else if(sb.charAt(i) == \')\' && count>0){\n                count--;\n            } else if(sb.charAt(i) == \')\' && count==0){\n                sb.delete(i,i+1);\n                i--;\n            }\n        }\n        for(int i=sb.length()-1;i>=0;i--){\n            if(count==0){\n                break;\n            }\n            if(sb.charAt(i)==\'(\'){\n                sb.delete(i,i+1);\n                count--;\n            }\n        }\n        return sb.toString();\n    }\n}\n```\n\n![_e5051e7d-7500-4812-a9c9-da39371e42fa.jpeg](https://assets.leetcode.com/users/images/c2f37baf-a59d-40c1-9f35-4ef0015dd1b9_1712386617.708701.jpeg)\n
['String', 'Stack', 'Java']