Skip to content

Commit

Permalink
day 7 simplify: process input from front instead of back
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Apr 3, 2019
1 parent 5a02664 commit 275ac7b
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions day7/problem.go
Expand Up @@ -3,15 +3,13 @@ package day7
// 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 == 0 || length == 1 {
if length := len(str); length < 2 {
return 1
}
var count int
if last := str[len(str)-1]; last > '0' && last <= '9' {
count = NumberOfDecodings(str[:len(str)-1])
}
if second := str[len(str)-2]; second == '1' || second == '2' && str[len(str)-1] < '7' {
count += NumberOfDecodings(str[:len(str)-2])
if first := str[0]; first == '1' || (first == '2' && str[1] < '7') {
count = NumberOfDecodings(str[2:])
}
count += NumberOfDecodings(str[1:])
return count
}

0 comments on commit 275ac7b

Please sign in to comment.