Home

# Daily Coding Problem #7 - Encoded Message

February 27, 2019

## Problem

Given the mapping a = 1, b = 2, … z = 26, and an encoded message, count the number of ways it can be decoded.

For example, the message ‘111’ would give 3, since it could be decoded as ‘aaa’, ‘ka’, and ‘ak’.

You can assume that the messages are decodable. For example, ‘001’ is not allowed.

## Solution

7_encoded_message.go

``````package daily_coding_problem_in_go

import (
"strconv"
)

func PossibleEncodedSolutions(message string) int {
length := len(message)
num, _ := strconv.Atoi(message)

if length == 1 {
return 1
}

if length == 2 && num > 0 && num < 27 {
return 2
}

if length == 2 {
return 1
}

return PossibleEncodedSolutions(message[1:]) + PossibleEncodedSolutions(message[2:])
}

``````

7_encoded_message_test.go

``````package daily_coding_problem_in_go

import "testing"

var encodeTests = []struct {
in  string
out int
}{
{"11", 2},
{"111", 3},
{"12", 2},
{"123", 3},
}

func TestPossibleEncodedSolutions(t *testing.T) {
for _, tc := range encodeTests {
if actual := PossibleEncodedSolutions(tc.in); actual != tc.out {
t.Errorf("%v expected %v, got %v", tc.in, tc.out, actual)
}
}
}

``````