#678
Valid Parenthesis String
expert · 1005 · lc medium +32 · verified · 39.8% accepted · 6,949 likes · top 19%
Description
A string s containing only '(', ')', and '*' is valid according to these rules:
- Every '(' must be matched by a ')'.
- Every ')' must be preceded by a matching '('.
- '(' must appear before its matching ')'.
- '*' may act as '(', as ')', or as an empty string "".
Return true if s is a valid string.
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "(*)"
Output: true
Example 3:
Input: s = "(*))"
Output: true
Code
1
2
3