678. Valid Parenthesis String
Valid Parenthesis String (LeetCode 678) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Stack Patterns pattern, in the Valid Parentheses Matching subpattern.
How to solve Valid Parenthesis String
Valid Parenthesis String is a Valid Parentheses problem, part of Stack Patterns. Master parentheses validation and balancing using stack. Handle multiple bracket types and minimum additions.
Work towards the solution in these steps:
- Track how many unmatched opening parentheses could remain after scanning each prefix.
- A wildcard can either increase that count, decrease it, or leave it unchanged, so maintain a range of possible counts rather than choosing one interpretation immediately.
- If even the largest possible count becomes negative, the current prefix cannot be valid under any interpretation.
The reference solution runs in O(n) time and O(1) space.
The Valid Parenthesis String solution and editorial walks through the full approach with reference code.
Problem statement
Given a string s, determine whether it can represent a correctly balanced sequence of parentheses. The string contains opening parentheses (, closing parentheses ), and wildcard characters .
Each may be interpreted as a single (, a single ), or an empty character. All non-wildcard characters retain their original meaning, and each wildcard receives exactly one of these interpretations.
Return true if some interpretation of every produces a valid parenthesis sequence, and return false otherwise. A valid sequence has matching pairs in the proper order, with no closing parenthesis appearing before its matching opening parenthesis.
Example 1:
Input: s = "(()" Output: true Explanation: Interpret as ), producing (()), which is balanced.
Example 2:
Input: s = "())" Output: false Explanation: The first three characters already leave one unmatched closing parenthesis, and the final wildcard cannot repair that ordering.
Example 3:
Input: s = "" Output: true Explanation: For instance, interpret the three wildcards as (, ), and an empty character to obtain ().
Constraints:
- 1 <= s.length <= 10^5 - Each character in s is (, ), or . - Each represents at most one character in the interpreted sequence. - The input string is provided as a JSON string.
Topics and companies
Topics: Greedy, Stack, String.
How to practise Valid Parenthesis String on Thita.ai
Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.
Open Valid Parenthesis String in the code editor, or read the Valid Parenthesis String editorial for a worked solution with its approach and complexity analysis.
Where Valid Parenthesis String sits in the DSA pattern sheet
Problems related to Valid Parenthesis String
Other problems that use the same Valid Parentheses Matching and Stack Patterns techniques:
- Longest Valid Parentheses — Hard, same subpattern
- Minimum Add to Make Parentheses Valid — Medium, same subpattern
- Minimum Number of Swaps to Make the String Balanced — Medium, same subpattern
- Minimum Remove to Make Valid Parentheses — Medium, same subpattern
- Valid Parentheses — Easy, same subpattern
- Asteroid Collision — Medium, same pattern
Valid Parenthesis String FAQ
What is the LeetCode number of Valid Parenthesis String?
Valid Parenthesis String is LeetCode problem 678, rated Medium.
What pattern is used to solve Valid Parenthesis String?
Valid Parenthesis String is a Valid Parentheses problem. It belongs to the Stack Patterns pattern, in the Valid Parentheses Matching subpattern.
What is the time complexity of Valid Parenthesis String?
The reference solution runs in O(n) time and O(1) space.