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