Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 81
Good morning! Here's your coding interview problem for today.

This problem was asked by Yelp.

Given a mapping of digits to letters (as in a phone number), and a digit string, return all possible letters the number could represent. You can assume each valid number in the mapping is a single digit.

For example if {“2”: [“a”, “b”, “c”], 3: [“d”, “e”, “f”], …} then “23” should return [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf"].

Pull request description:
Day81
Resolves #171.

Patch:
diff --git a/README.md b/README.md
index 41acb17..9f57cd0 100644
--- a/README.md
+++ b/README.md
@@ -90,3 +90,4 @@ problems from
 * [Day 78](https://github.com/vaskoz/dailycodingproblem-go/issues/165)
 * [Day 79](https://github.com/vaskoz/dailycodingproblem-go/issues/167)
 * [Day 80](https://github.com/vaskoz/dailycodingproblem-go/issues/168)
+* [Day 81](https://github.com/vaskoz/dailycodingproblem-go/issues/171)
diff --git a/day81/problem.go b/day81/problem.go
new file mode 100644
index 0000000..4df735e
--- /dev/null
+++ b/day81/problem.go
@@ -0,0 +1,26 @@
+package day81
+
+// DigitMappings represents digits that can be mapped to multiple other values.
+// Think of the alpha codes on a phone pad.
+type DigitMappings map[rune][]string
+
+// PossiblePhoneLetters takes a given phone number and returns all possible letter
+// combinations.
+func PossiblePhoneLetters(number string, digitMappings DigitMappings) []string {
+	var result []string
+	for i, r := range number {
+		letters := digitMappings[r]
+		if i == 0 {
+			result = append(result, letters...)
+		} else {
+			tmp := make([]string, 0, len(letters)*len(result))
+			for _, part := range result {
+				for _, letter := range letters {
+					tmp = append(tmp, part+letter)
+				}
+			}
+			result = tmp
+		}
+	}
+	return result
+}


Test patch:
diff --git a/day81/problem_test.go b/day81/problem_test.go
new file mode 100644
index 0000000..90859db
--- /dev/null
+++ b/day81/problem_test.go
@@ -0,0 +1,32 @@
+package day81
+
+import (
+	"reflect"
+	"testing"
+)
+
+var testcases = []struct {
+	number   string
+	mapping  DigitMappings
+	expected []string
+}{
+	{"23", DigitMappings{'2': {"a", "b", "c"}, '3': {"d", "e", "f"}},
+		[]string{"ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"}},
+}
+
+func TestPossiblePhoneLetters(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := PossiblePhoneLetters(tc.number, tc.mapping); !reflect.DeepEqual(tc.expected, result) {
+			t.Errorf("Expected %v got %v", tc.expected, result)
+		}
+	}
+}
+
+func BenchmarkPossiblePhoneLetters(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			PossiblePhoneLetters(tc.number, tc.mapping)
+		}
+	}
+}