Skip to content

Commit 5f4b3a5

Browse files
committed
Add problem 678 - Valid Parenthesis Substring
1 parent c99239c commit 5f4b3a5

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package greedy
2+
3+
func ValidParenthesisString(s string) bool {
4+
// Minimum and maximum count of open parentheses
5+
minOpen, maxOpen := 0, 0
6+
// Traverse through the string
7+
for _, c := range s {
8+
9+
if c == '(' {
10+
// If it is a left parenthesis, it means you have one
11+
// more open parenthesis to worry about
12+
minOpen++
13+
maxOpen++
14+
} else if c == ')' {
15+
// If it is right parenthesis, it means you have one
16+
// less open parenthesis to worry about
17+
minOpen--
18+
maxOpen--
19+
} else if c == '*' {
20+
// If it is *, it means there are three cases
21+
// 1. If we make it as ), then we have one less open parenthesis to worry about
22+
// 2. If we make it as (, then we have one more open parentheses
23+
// to worry about
24+
// 3. No impact at all
25+
minOpen--
26+
maxOpen++
27+
}
28+
29+
// There are more closing parentheses, then opening ones
30+
if maxOpen < 0 {
31+
return false
32+
}
33+
34+
// We can't have negative open parenthesis count
35+
minOpen = max(minOpen, 0)
36+
}
37+
return minOpen == 0
38+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package greedy_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/anirudhology/leetcode-go/problems/greedy"
7+
)
8+
9+
func TestCheckValidString(t *testing.T) {
10+
// Test null input
11+
if !greedy.ValidParenthesisString("") {
12+
t.Errorf("Expected true for empty string")
13+
}
14+
15+
// Test valid strings
16+
if !greedy.ValidParenthesisString("()") {
17+
t.Errorf("Expected true for ()")
18+
}
19+
if !greedy.ValidParenthesisString("(*)") {
20+
t.Errorf("Expected true for (*)")
21+
}
22+
if !greedy.ValidParenthesisString("(*))") {
23+
t.Errorf("Expected true for (*))")
24+
}
25+
26+
// Test invalid strings
27+
if greedy.ValidParenthesisString(")") {
28+
t.Errorf("Expected false for )")
29+
}
30+
if greedy.ValidParenthesisString(")(") {
31+
t.Errorf("Expected false for )(")
32+
}
33+
if !greedy.ValidParenthesisString("((*)") {
34+
t.Errorf("Expected false for ((*)")
35+
}
36+
if greedy.ValidParenthesisString("((*))(") {
37+
t.Errorf("Expected false for ((*))(")
38+
}
39+
}
40+
41+
func TestEdgeCases(t *testing.T) {
42+
// Test string with only one *
43+
if !greedy.ValidParenthesisString("*") {
44+
t.Errorf("Expected true for *")
45+
}
46+
47+
// Test string with multiple *
48+
if !greedy.ValidParenthesisString("***") {
49+
t.Errorf("Expected true for ***")
50+
}
51+
52+
// Test strings with nested parentheses
53+
if !greedy.ValidParenthesisString("(())") {
54+
t.Errorf("Expected true for (())")
55+
}
56+
if greedy.ValidParenthesisString("(()))") {
57+
t.Errorf("Expected false for (()))")
58+
}
59+
}

0 commit comments

Comments
 (0)