Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 22
Good morning! Here's your coding interview problem for today.

This problem was asked by Microsoft.

Given a dictionary of words and a string made up of those words (no spaces), return the original sentence in a list. If there is more than one possible reconstruction, return any of them. If there is no possible reconstruction, then return null.

For example, given the set of words 'quick', 'brown', 'the', 'fox', and the string "thequickbrownfox", you should return ['the', 'quick', 'brown', 'fox'].

Given the set of words 'bed', 'bath', 'bedbath', 'and', 'beyond', and the string "bedbathandbeyond", return either ['bed', 'bath', 'and', 'beyond] or ['bedbath', 'and', 'beyond'].

Pull request description:
Day22
Resolves #53.

Patch:
diff --git a/README.md b/README.md
index 9d58811..a18d8d4 100644
--- a/README.md
+++ b/README.md
@@ -35,3 +35,4 @@ problems from
 * [Day 19 Sudoku Solver](https://github.com/vaskoz/dailycodingproblem-go/issues/47)
 * [Day 20](https://github.com/vaskoz/dailycodingproblem-go/issues/49)
 * [Day 21](https://github.com/vaskoz/dailycodingproblem-go/issues/51)
+* [Day 22](https://github.com/vaskoz/dailycodingproblem-go/issues/53)
diff --git a/day22/problem.go b/day22/problem.go
new file mode 100644
index 0000000..ca5b518
--- /dev/null
+++ b/day22/problem.go
@@ -0,0 +1,28 @@
+package day22
+
+import (
+	"errors"
+	"fmt"
+	"strings"
+)
+
+// WordSet represents a set of possible words.
+type WordSet map[string]struct{}
+
+var errNoSentencePossible = errors.New("No sentence can be built")
+
+// OriginalSentence returns the original sentence from a concatenated sentence.
+func OriginalSentence(words WordSet, noSpaces string) (string, error) {
+	if len(noSpaces) == 0 {
+		return "", nil
+	}
+	for word := range words {
+		if len(word) <= len(noSpaces) && word == noSpaces[:len(word)] {
+			rest, err := OriginalSentence(words, noSpaces[len(word):])
+			if err == nil {
+				return strings.TrimSpace(fmt.Sprintf("%s %s", word, rest)), nil
+			}
+		}
+	}
+	return "", errNoSentencePossible
+}


Test patch:
diff --git a/day22/problem_test.go b/day22/problem_test.go
new file mode 100644
index 0000000..177c2c4
--- /dev/null
+++ b/day22/problem_test.go
@@ -0,0 +1,32 @@
+package day22
+
+import "testing"
+
+var testcases = []struct {
+	words        WordSet
+	concatenated string
+	expected     string
+	expectedErr  error
+}{
+	{WordSet{"quick": struct{}{}, "brown": struct{}{}, "fox": struct{}{}, "the": struct{}{}},
+		"thequickbrownfox", "the quick brown fox", nil},
+	{WordSet{"quick": struct{}{}, "brown": struct{}{}, "fox": struct{}{}, "the": struct{}{}},
+		"thequickbrownfoxy", "", errNoSentencePossible},
+}
+
+func TestOriginalSentence(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result, err := OriginalSentence(tc.words, tc.concatenated); result != tc.expected || err != tc.expectedErr {
+			t.Errorf("Expected (%v, %v) but got (%v, %v)", tc.expected, tc.expectedErr, result, err)
+		}
+	}
+}
+
+func BenchmarkOriginalSentence(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			OriginalSentence(tc.words, tc.concatenated)
+		}
+	}
+}