Ques: A paranthesis string only consists of '(' and ')'. In a given input, the difference between the number of ')' and '(' is always one. This means that there might be one more '(' than the number of ')' present in a string or vice versa. Your task is to calculate the number of ways you can add a single ')' - or '(' depending on the string - to make the paranthesis string balanced and valid. The definition of a valid balanced parantheses string is the same as the definition in this question: https://leetcode.com/problems/valid-parentheses/.
Examples:
- If the given input string is '())', then the output is 3. This because we can add a single '(' in two ways: '(())', '(())' and '()()'.
- If the given input string is ')((', then the output is 0. This is because there is no way to add a single ')' to the given parantheses string to make it valid and balanced.
Time Limit to Solve: 30 minutes
Notes: One of the most difficult questions I have faced in an interview. Even after scouring the internet and troubling my peers, I have still not found an optimal solution to this problem. On the face of it, it seems like an easy question, but it is LC-Hard. During the interview, I was able to solve it via brute force by adding a paranthesis at every postition and checking whether it was valid.
If my post needs any clarification or improvement, please let me know in the comments. Upvote if you found this post useful.


