-
Notifications
You must be signed in to change notification settings - Fork 32
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
3 changed files
with
62 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,24 @@ | ||
package day142 | ||
|
||
// WildcardParens answers if the parens are balanced. | ||
// This includes a wildcard character '*' that can be either paren | ||
// or an empty string. | ||
func WildcardParens(input string) bool { | ||
var opens, wildcards int | ||
for _, c := range input { | ||
if c == '(' { | ||
opens++ | ||
} else if c == ')' { | ||
if opens == 0 && wildcards < 1 { | ||
return false | ||
} else if opens == 0 { | ||
wildcards-- | ||
} else { | ||
opens-- | ||
} | ||
} else if c == '*' { | ||
wildcards++ | ||
} | ||
} | ||
return opens == 0 || opens <= wildcards | ||
} |
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,37 @@ | ||
package day142 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
input string | ||
balanced bool | ||
}{ | ||
{"(()*", true}, | ||
{"(*)", true}, | ||
{")*(", false}, | ||
{"(((**", false}, | ||
{"((***))", true}, | ||
{"(***)))", true}, | ||
{"**))**()))*", true}, | ||
{"************(*)***********", true}, | ||
{"***********************", true}, | ||
{"****)))", true}, | ||
{"****)))))", false}, | ||
} | ||
|
||
func TestWildcardParens(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if balanced := WildcardParens(tc.input); balanced != tc.balanced { | ||
t.Errorf("Given %v Expected %v got %v", tc.input, tc.balanced, balanced) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkWildcardParens(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
WildcardParens(tc.input) | ||
} | ||
} | ||
} |