-
Notifications
You must be signed in to change notification settings - Fork 382
Description
LeetCode Username
Problem Number, Title, and Link
Bug Category
Problem description, Problem examples, Problem constraints, Problem hints, Incorrect or missing "Related Topics", Incorrect test case (Output of test case is incorrect as per the problem statement), Missing test case (Incorrect/Inefficient Code getting accepted because of missing test cases), Editorial
Bug Description
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 "".
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "(*)"
Output: true
Example 3:
Input: s = "(*))"
Output: true
Language Used for Code
Java
Code used for Submit/Run operation
class Solution {
public boolean checkValidString(final String s) {
int low = 0;
int high = 0;
for (final char c : s.toCharArray()) {
switch (c) {
case '(':
++low;
++high;
break;
case ')':
low = Math.max(0, --low);
--high;
break;
case '*':
low = Math.max(0, --low);
++high;
break;
}
if (high < 0)
return false;
}
return low == 0;
}
}
