Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 910792a

Browse files
authoredJan 17, 2022
Update 2116-check-if-a-parentheses-string-can-be-validcheck-if-a-parentheses-string-can-be-valid.js
1 parent 5d1a838 commit 910792a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
 

‎2116-check-if-a-parentheses-string-can-be-validcheck-if-a-parentheses-string-can-be-valid.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} locked
4+
* @return {boolean}
5+
*/
6+
const canBeValid = function(s, locked) {
7+
const n = s.length
8+
if(n % 2 === 1) return false
9+
let x = 0
10+
for(let i = 0; i < n; i++) {
11+
if(s[i] === '(' || locked[i] === '0') x++
12+
else if(x > 0) x--
13+
else return false
14+
}
15+
x = 0
16+
for(let i = n - 1; i >= 0; i--) {
17+
if(s[i] === ')' || locked[i] === '0') x++
18+
else if(x > 0) x--
19+
else return false
20+
}
21+
return true
22+
};
23+
24+
// another
25+
126
/**
227
* @param {string} s
328
* @param {string} locked

0 commit comments

Comments
 (0)
Please sign in to comment.