Skip to content

Commit 14e7775

Browse files
authored
Merge pull request #1 from LuaanNguyen/daily-leetcode-2116
Daily LeetCode 2116. Check if a Parentheses String Can Be Valid
2 parents b9a6311 + dcade34 commit 14e7775

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package check_if_a_parentheses_string_can_be_valid
2+
3+
func canBeValid(s string, locked string) bool {
4+
if len(s) % 2 == 1 {
5+
return false
6+
}
7+
8+
openBrackets := make([]int, 0)
9+
unlocked := make([]int, 0)
10+
11+
for i, _ := range s {
12+
if locked[i] == '0' {
13+
unlocked = append(unlocked, i)
14+
} else if s[i] == '(' {
15+
openBrackets = append(openBrackets, i)
16+
} else if s[i] == ')' {
17+
if len(openBrackets) > 0 {
18+
openBrackets = openBrackets[:len(openBrackets) - 1]
19+
} else if len(unlocked) > 0 {
20+
unlocked = unlocked[:len(unlocked) - 1]
21+
} else {
22+
return false
23+
}
24+
}
25+
}
26+
27+
for {
28+
if len(openBrackets) > 0 && len(unlocked) > 0 && openBrackets[len(openBrackets) - 1] < unlocked[len(unlocked) - 1] {
29+
openBrackets = openBrackets[:len(openBrackets) - 1]
30+
unlocked = unlocked[:len(unlocked) - 1]
31+
} else {
32+
break
33+
}
34+
}
35+
36+
return len(openBrackets) == 0
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package check_if_a_parentheses_string_can_be_valid
2+
3+
import "testing"
4+
5+
6+
func TestCanBeValid(t *testing.T) {
7+
tests := []struct {
8+
name string
9+
s string
10+
locked string
11+
expectedResult bool
12+
} {
13+
{
14+
name: "Test Case 1",
15+
s: "))()))",
16+
locked: "010100",
17+
expectedResult: true,
18+
},
19+
{
20+
name: "Test Case 2",
21+
s: "()()",
22+
locked: "010100",
23+
expectedResult: true,
24+
},
25+
}
26+
27+
for _, test := range tests {
28+
t.Run(test.name, func (t * testing.T) {
29+
got := canBeValid(test.s, test.locked)
30+
if got != test.expectedResult {
31+
t.Errorf("canBeValidConstruct(\"%s\", %s) = %t; want %t", test.s, test.locked, got, test.expectedResult)
32+
}
33+
})
34+
}
35+
}

0 commit comments

Comments
 (0)