-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path0678-valid-parenthesis-string.js
More file actions
33 lines (30 loc) · 980 Bytes
/
0678-valid-parenthesis-string.js
File metadata and controls
33 lines (30 loc) · 980 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* 678. Valid Parenthesis String
* https://leetcode.com/problems/valid-parenthesis-string/
* Difficulty: Medium
*
* 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 "".
*/
/**
* @param {string} s
* @return {boolean}
*/
var checkValidString = function(s) {
let result = 0;
let offset = 0;
for (const character of s) {
result += character === '(' ? 1 : -1;
offset += character !== ')' ? 1 : -1;
if (offset < 0) return false;
result = Math.max(0, result);
}
return !result;
};