Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 266
Good morning! Here's your coding interview problem for today.

This problem was asked by Pivotal.

A step word is formed by taking a given word, adding a letter, and anagramming the result. For example, starting with the word "APPLE", you can add an "A" and anagram to get "APPEAL".

Given a dictionary of words and an input word, create a function that returns all valid step words.

Pull request description:
Day266
Resolves #542.

Patch:
diff --git a/README.md b/README.md
index fb93a8a..4c5f142 100644
--- a/README.md
+++ b/README.md
@@ -271,4 +271,5 @@ problems from
 * [Day 257](https://github.com/vaskoz/dailycodingproblem-go/issues/529)
 * [Day 258](https://github.com/vaskoz/dailycodingproblem-go/issues/530)
 * [Day 260](https://github.com/vaskoz/dailycodingproblem-go/issues/533)
+* [Day 266](https://github.com/vaskoz/dailycodingproblem-go/issues/542)
 * [Day 268](https://github.com/vaskoz/dailycodingproblem-go/issues/544)
diff --git a/day266/problem.go b/day266/problem.go
new file mode 100644
index 0000000..a833d42
--- /dev/null
+++ b/day266/problem.go
@@ -0,0 +1,34 @@
+package day266
+
+import "reflect"
+
+// AllValidStepWords returns all valid step words by anagramming
+// an appended letter.
+// Runs in O(dict*26)
+func AllValidStepWords(dict []string, input string) []string {
+	dictAnagramData := make([]map[rune]int, len(dict))
+	for i, word := range dict {
+		dictAnagramData[i] = make(map[rune]int)
+		for _, r := range word {
+			dictAnagramData[i][r]++
+		}
+	}
+	inputAnagramData := make(map[rune]int)
+	for _, r := range input {
+		inputAnagramData[r]++
+	}
+	var result []string
+	for r := 'a'; r <= 'z'; r++ {
+		inputAnagramData[r]++
+		for i, data := range dictAnagramData {
+			if reflect.DeepEqual(data, inputAnagramData) {
+				result = append(result, dict[i])
+			}
+		}
+		inputAnagramData[r]--
+		if inputAnagramData[r] == 0 {
+			delete(inputAnagramData, r)
+		}
+	}
+	return result
+}


Test patch:
diff --git a/day266/problem_test.go b/day266/problem_test.go
new file mode 100644
index 0000000..b43777e
--- /dev/null
+++ b/day266/problem_test.go
@@ -0,0 +1,32 @@
+package day266
+
+import (
+	"reflect"
+	"testing"
+)
+
+// nolint
+var testcases = []struct {
+	dict           []string
+	input          string
+	validStepWords []string
+}{
+	{[]string{"appeal", "apearr", "appear"}, "apple", []string{"appeal"}},
+}
+
+func TestAllValidStepWords(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := AllValidStepWords(tc.dict, tc.input); !reflect.DeepEqual(result, tc.validStepWords) {
+			t.Errorf("Expected %v, got %v", tc.validStepWords, result)
+		}
+	}
+}
+
+func BenchmarkAllValidStepWords(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			AllValidStepWords(tc.dict, tc.input)
+		}
+	}
+}