Valid parenthesis and range sum of BST
Given a string text
consisting of lowercase letters and parentheses characters '('
and ')'
, your goal is to remove the fewest possible parentheses (either '('
or ')'
) from any positions so that the resulting string is correctly balanced.
A balanced parentheses string is defined as:
Input: text = "a(b(c)d)e)" Output: "a(b(c)d)e" Explanation: "a(b(cd)e)" or "ab(c)d)e" would also be valid outputs. Input: text = "x)y(z" Output: "xy(z" Input: text = ")))(((" Output: "" Explanation: Removing all parentheses results in a balanced empty string. Input: text = "(h(e(l)l)o" Output: "h(e(l)l)o"
text
is between 1 and 120,000 characters.text[i]
is either a lowercase letter, '('
, or ')'
.