Skip to main content
added 93 characters in body; edited tags; edited title
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Leetcode problem optimisation : Minimum Removeminimum removal to Make Valid Parenthesesmake valid parentheses

Question

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 = "))((" Output: "" Explanation: An empty string is also valid. 

Question

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 = "))((" Output: "" Explanation: An empty string is also valid. 

Leetcode problem optimisation : Minimum Remove to Make Valid Parentheses

Question

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 = "))((" Output: "" Explanation: An empty string is also valid. 

Leetcode problem: minimum removal to make valid parentheses

Question

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 = "))((" Output: "" Explanation: An empty string is also valid. 
Source Link
D_S_X
  • 189
  • 7

Leetcode problem optimisation : Minimum Remove to Make Valid Parentheses

I was working on this problem on leetcode

Question

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 = "))((" Output: "" Explanation: An empty string is also valid. 

My Solution

var minRemoveToMakeValid = function(s) { let myStack = []; let myObj = {}; // for storing index having invalid parentheses to be removed let out = ''; // O(n)?? for(let i = 0; i < s.length; i++){ let val = s[i] if(["(", ")"].includes(val)){ if(val == ')' && myStack[myStack.length -1]?.char == "("){ const rem = myStack.pop() delete myObj[rem.index] } else { myStack.push({ char: val, index: i }) myObj[i] = true } } } // O(n) for(let i = 0; i < s.length; i++){ if(!myObj[i]){ out += s[i] } } return out }; 

Isn't the above solution in O(N) time complexity? It seems so to me however the results show that it is not (the solution runtime is in bottom 8% percentile)

Why is this having such bad runtime even it seems like O(N)? or is it not so?