Skip to content

Commit

Permalink
Merge 9bf72ea into 66d338e
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Mar 7, 2020
2 parents 66d338e + 9bf72ea commit 728dddc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,5 +462,6 @@ problems from
* [Day 463](https://github.com/vaskoz/dailycodingproblem-go/issues/937)
* [Day 464](https://github.com/vaskoz/dailycodingproblem-go/issues/935)
* [Day 465](https://github.com/vaskoz/dailycodingproblem-go/issues/933)
* [Day 472](https://github.com/vaskoz/dailycodingproblem-go/issues/941)
* [Day 473](https://github.com/vaskoz/dailycodingproblem-go/issues/939)

17 changes: 17 additions & 0 deletions day472/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package day472

// NumberOfDecodings calculates the number of possible decodings where
// letters are encoding as a=1, b=2 ... z=26
func NumberOfDecodings(str string) int {
if length := len(str); length < 2 {
return 1
}

count := 0

if first := str[0]; first == '1' || (first == '2' && str[1] < '7') {
count = NumberOfDecodings(str[2:])
}

return count + NumberOfDecodings(str[1:])
}
33 changes: 33 additions & 0 deletions day472/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package day472

import "testing"

// nolint
var testcases = []struct {
input string
expected int
}{
{"111", 3},
{"1111", 5},
{"2626", 4},
{"1234", 3},
{"1111127", 13},
}

func TestNumberOfDecodings(t *testing.T) {
t.Parallel()

for _, tc := range testcases {
if result := NumberOfDecodings(tc.input); result != tc.expected {
t.Errorf("For input %v, expected %v but got %v", tc.input, tc.expected, result)
}
}
}

func BenchmarkNumberOfDecodings(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
NumberOfDecodings(tc.input)
}
}
}

0 comments on commit 728dddc

Please sign in to comment.