-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution.cpp
43 lines (40 loc) · 957 Bytes
/
solution.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
class Solution
{
public:
bool canBeValid(string s, string locked)
{
if (s.size() % 2 != 0)
return false; // Odd length can't be balanced
int open = 0, flexible = 0;
// Left-to-right pass
for (int i = 0; i < s.size(); i++)
{
if (locked[i] == '1')
{
open += (s[i] == '(' ? 1 : -1);
}
else
{
flexible++;
}
if (open + flexible < 0)
return false;
}
open = 0, flexible = 0;
// Right-to-left pass
for (int i = s.size() - 1; i >= 0; i--)
{
if (locked[i] == '1')
{
open += (s[i] == ')' ? 1 : -1);
}
else
{
flexible++;
}
if (open + flexible < 0)
return false;
}
return true;
}
};