forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck if a Parentheses String Can Be Valid.cpp
52 lines (36 loc) · 1.38 KB
/
Check if a Parentheses String Can Be Valid.cpp
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
// Runtime: 117 ms (Top 72.95%) | Memory: 27.1 MB (Top 80.99%)
class Solution {
public:
bool canBeValid(string s, string locked) {
int n = s.size();
if(n&1) return false;
int balance = 0;
// First check balance from left to right
// For opening '(' brackets
for(int i=0; i<n; i++) {
// If either char is ( or it is unlocked,
// We can increment balance
if(locked[i] == '0' || s[i] == '(') balance++;
else balance--; // otherwise decrement balance, since it is ) and also locked
// Since balance is negative, we have more ')'.
// And there is no unlocked char available
// SO, it is invalid string for sure
if(balance < 0) return false;
}
// reset balance
balance = 0;
// Then also check balance from right to left
// For closing ')' brackets
for(int i=n-1; i>=0; i--) {
// If either char is ) or it is unlocked,
// We can increment balance
if(locked[i] == '0' || s[i] == ')') balance++;
else balance--;
// Since balance is negative, we have more '('.
// And there is no unlocked char available
// SO, it is invalid string for sure
if(balance < 0) return false;
}
return true;
}
};