-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path2116-check-if-a-parentheses-string-can-be-valid.js
54 lines (50 loc) · 1.46 KB
/
2116-check-if-a-parentheses-string-can-be-valid.js
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* 2116. Check if a Parentheses String Can Be Valid
* https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/
* Difficulty: Medium
*
* A parentheses string is a non-empty string consisting only of '(' and ')'. It is valid
* if any of the following conditions is true:
* - It is ().
* - It can be written as AB (A concatenated with B), where A and B are valid parentheses
* strings.
* - It can be written as (A), where A is a valid parentheses string.
*
* You are given a parentheses string s and a string locked, both of length n. locked is a
* binary string consisting only of '0's and '1's. For each index i of locked,
* - If locked[i] is '1', you cannot change s[i].
* - But if locked[i] is '0', you can change s[i] to either '(' or ')'.
*
* Return true if you can make s a valid parentheses string. Otherwise, return false.
*/
/**
* @param {string} s
* @param {string} locked
* @return {boolean}
*/
var canBeValid = function(s, locked) {
if (s.length % 2) return false;
let symmetrical = 0;
for (let i = 0; i < s.length; i++) {
if (locked[i] === '0' || s[i] === '(') {
symmetrical++;
} else {
symmetrical--;
}
if (symmetrical < 0) {
return false;
}
}
symmetrical = 0;
for (let i = s.length - 1; i >= 0; i--) {
if (locked[i] === '0' || s[i] === ')') {
symmetrical++;
} else {
symmetrical--;
}
if (symmetrical < 0) {
return false;
}
}
return true;
};