-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package parentheses checks if a string has balanced parentheses. | ||
package parentheses |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package parentheses | ||
|
||
func IsBalanced(s string) bool { | ||
low, high := 0, 0 | ||
for _, char := range s { | ||
switch char { | ||
case '(': | ||
low++ | ||
high++ | ||
case ')': | ||
low = max(low-1, 0) | ||
high-- | ||
case '*': | ||
low = max(low-1, 0) | ||
high++ | ||
} | ||
|
||
if high < 0 { | ||
return false | ||
} | ||
} | ||
|
||
return low == 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package parentheses_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/thenativeweb/codingcircle/parentheses" | ||
) | ||
|
||
func TestIsBalanced(t *testing.T) { | ||
t.Run("empty string", func(t *testing.T) { | ||
assert.True(t, parentheses.IsBalanced("")) | ||
}) | ||
|
||
t.Run("balanced string", func(t *testing.T) { | ||
assert.True(t, parentheses.IsBalanced("()")) | ||
assert.True(t, parentheses.IsBalanced("(())")) | ||
assert.True(t, parentheses.IsBalanced("()()")) | ||
assert.True(t, parentheses.IsBalanced("(()())")) | ||
assert.True(t, parentheses.IsBalanced("((()))")) | ||
}) | ||
|
||
t.Run("unbalanced string", func(t *testing.T) { | ||
assert.False(t, parentheses.IsBalanced("(")) | ||
assert.False(t, parentheses.IsBalanced(")")) | ||
assert.False(t, parentheses.IsBalanced(")(")) | ||
assert.False(t, parentheses.IsBalanced("())")) | ||
assert.False(t, parentheses.IsBalanced("(()")) | ||
assert.False(t, parentheses.IsBalanced("((())")) | ||
}) | ||
|
||
t.Run("balanced string with wildcard", func(t *testing.T) { | ||
assert.True(t, parentheses.IsBalanced("*")) | ||
assert.True(t, parentheses.IsBalanced("(*)")) | ||
assert.True(t, parentheses.IsBalanced("(*))")) | ||
assert.True(t, parentheses.IsBalanced("(**))*")) | ||
assert.True(t, parentheses.IsBalanced("*((**))*")) | ||
assert.True(t, parentheses.IsBalanced("(***")) | ||
}) | ||
|
||
t.Run("unbalanced string with wildcard", func(t *testing.T) { | ||
assert.False(t, parentheses.IsBalanced("*(")) | ||
assert.False(t, parentheses.IsBalanced("*)(")) | ||
assert.False(t, parentheses.IsBalanced("***(")) | ||
assert.False(t, parentheses.IsBalanced("(***(()")) | ||
assert.False(t, parentheses.IsBalanced("(**(")) | ||
}) | ||
} |