3
bool canBeValid(string s, string locked) {5
if (n & 1) return false;9
// First check balance from left to right10
// For opening '(' brackets12
for (int i = 0; i < n; i++) {13
// If either char is ( or it is unlocked,14
// We can increment balance16
if (locked[i] == '0' || s[i] == '(')19
balance--; // otherwise decrement balance, since it is ) and also locked21
// Since balance is negative, we have more ')'.22
// And there is no unlocked char available23
// SO, it is invalid string for sure25
if (balance < 0) return false;31
// Then also check balance from right to left32
// For closing ')' brackets34
for (int i = n - 1; i >= 0; i--) {35
// If either char is ) or it is unlocked,36
// We can increment balance38
if (locked[i] == '0' || s[i] == ')')43
// Since balance is negative, we have more '('.44
// And there is no unlocked char available45
// SO, it is invalid string for sure47
if (balance < 0) return false;