Valid Parenthesis String
Learn how to solve the 'Valid Parenthesis String' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Given a string s containing only three types of characters: '(', ')' and '*', return True if s is valid.
The following rules define a valid string:
- Any left parenthesis '(' must have a corresponding right parenthesis ')'.
- Any right parenthesis ')' must have a corresponding left parenthesis '('.
- Left parenthesis '(' must go before the corresponding right parenthesis ')'.
- '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string.
Write a function checkValidString(s: str) -> bool.
- •1 <= len(s) <= 100
- •s contains only '(', ')' and '*'
Examples
s = "()"
True
Matches perfectly.
s = "(*)"
True
Treat '*' as empty string.
s = "(*))"
True
Treat '*' as '('.
Need a Hint?
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.