-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
27 lines (26 loc) · 759 Bytes
/
Solution.cs
File metadata and controls
27 lines (26 loc) · 759 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
public class Solution
{
public bool CanBeValid(string s, string locked)
{
if ((s.Length & 1) != 0) return false;
Stack<int> opened = [];
Stack<int> unLocked = [];
for (int i = 0; i < s.Length; i++)
{
if (locked[i] == '0') unLocked.Push(i);
else if (s[i] == '(') opened.Push(i);
else
{
if (opened.Count > 0) opened.Pop();
else if (unLocked.Count > 0) unLocked.Pop();
else return false;
}
}
while (opened.Count > 0 && unLocked.Count > 0 && opened.Peek() < unLocked.Peek())
{
opened.Pop();
unLocked.Pop();
}
return opened.Count == 0;
}
}