1249. Minimum Remove to Make Valid Parentheses

Given a string s of '(' , ')' and lowercase English characters.

Your task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string.

Formally, a parentheses string is valid if and only if:

    - It is the empty string, contains only lowercase characters, or
    - It can be written as AB (A concatenated with B), where A and B are valid strings, or
    - It can be written as (A), where A is a valid string.

Example 1:

Input: s = "lee(t(c)o)de)"
Output: "lee(t(c)o)de"
Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.

Example 2:

Input: s = "a)b(c)d"
Output: "ab(c)d"

Example 3:

Input: s = "))(("
Output: ""
Explanation: An empty string is also valid.

https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/


Question Asked in

Facebook


Video coming soon!



Subscribe for more updates


Preparing for interviews? IDeserve team is here to help.




Hone your coding skills!




AngryNerds Practice platform »

Code Snippet

			
package com.ideserve.saurabh.questions;

/**
 * <b>IDeserve <br>
 * <a href="https://www.youtube.com/c/IDeserve">https://www.youtube.com/c/IDeserve</a>
 * Given a string s of '(' , ')' and lowercase English characters. 
 * Your task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the 
 * resulting parentheses string is valid and return any valid string. 
 *
 * @author Sakshi
 */
public class Solution {
    
    public String minRemoveToMakeValid(String s) {
        if(s.length() == 0) {
            return s;
        }

        int n = s.length();
        int extraOpenParCount = 0;
        char[] res = new char[n];
        int j = 0;
        for (int i = 0; i < n; i++) {
            char ch = s.charAt(i);
            if ((ch >= 'a' && ch <= 'z') || ch == '(') {
                res[j++] = ch;
                if (ch == '(') extraOpenParCount++;
            } else if (ch == ')') {
                // Eliminates non-matching close parentheses here itself
                if (extraOpenParCount > 0) {
                    extraOpenParCount--;
                    res[j++] = ch;
                }
            }
        }
        
        // For cases like: ()( , we come from right to left or you end up deleting first open parentheses which will be invalid string
        j--;
        StringBuilder st = new StringBuilder();
        for (int i = j; i >= 0; i--) {
            if (extraOpenParCount > 0 && res[i] == '(') {
                extraOpenParCount--;
            } else {
                st.append(res[i]);
            }
        }        
        
        // using st.reverse makes the code faster than prefixing or insert at the beginning
        return st.reverse().toString();
    }
}
		

Order of the Algorithm

Time Complexity is O(n)
Space Complexity is O(n)


Contribution

  • Sincere thanks from IDeserve community to Sakshi Kataria for compiling current post.

    Sakshi Kataria

    Ninja Programmer