Problem
You get a string s that contains only the characters (, ) and *. Return true if s is valid. Otherwise, return false.
A string is balanced when each ( has a matching ) after it, and each ) has a matching ( before it. An empty string is balanced.
Each * can be a (, a ) or an empty string. The string s is valid if some choice for each * makes s balanced.
Example 1
- Input:
- s = "(*()"
- Output:
- true
- Explanation:
- Use
)for the*. The result is()(), which is balanced.
Example 2
- Input:
- s = ")*("
- Output:
- false
- Explanation:
- The first character is
), and no(comes before it. No choice for the*can fix this.
Example 3
- Input:
- s = "((**"
- Output:
- true
- Explanation:
- Use
)for each*. The result is(()).
Constraints
- 1 <= s.length <= 100
- s[i] is '(', ')' or '*'.
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Longest Valid Parentheses
Coding · Bracket Sequences · Dynamic Programming · Stack
- Minimum Add to Make Parentheses Valid
Coding · Bracket Sequences · Greedy · Stack
- Minimum Insertions to Balance a Parentheses String
Coding · Bracket Sequences · Greedy · Stack
- Maximum Nesting Depth of Two Valid Parentheses Strings
Coding · Bracket Sequences · Stack · String
Sources · 2 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
Apple
Leetcode 678. Valid Parenthesis String
Reported Jul 22, 2026 · 1 source report
STAFF
Source snapshot: Nov 12, 2026
View source
LeetCode company tags · 2 records
Microsoft
Valid Parenthesis String
Reported date not specified · Source window: 30d
Source snapshot: Oct 20, 2026
Source frequency score: 61.5. This is not an interview probability.
View sourceOracle
Valid Parenthesis String
Reported date not specified · Source window: 6mo
Source snapshot: Oct 20, 2026
Source frequency score: 74.2. This is not an interview probability.
View source