Skip to content

Commit

Permalink
Merge a519ecf into c271ec6
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Jan 11, 2019
2 parents c271ec6 + a519ecf commit f696d1b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,4 @@ problems from
* [Day 139](https://github.com/vaskoz/dailycodingproblem-go/issues/288)
* [Day 140](https://github.com/vaskoz/dailycodingproblem-go/issues/292)
* [Day 141](https://github.com/vaskoz/dailycodingproblem-go/issues/294)
* [Day 142](https://github.com/vaskoz/dailycodingproblem-go/issues/297)
24 changes: 24 additions & 0 deletions day142/problem.go
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
}
37 changes: 37 additions & 0 deletions day142/problem_test.go
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)
}
}
}

0 comments on commit f696d1b

Please sign in to comment.