We have a list of valid words, and a long string of text without whitespace. All characters are UTF8, a-z.
Please write a function that finds if the string can be composed of valid words, and if so, adds whitespace between them. Not all words have to be used, but words can be used multiple times. If it's not possible, return empty string and a descriptive error.
The words on validWords
do not overlap.
func addWhitespace(input string, validWords []string) (string, error)
When validWords is:
validWords := []string{"cat", "dogs", "abc", "fly", "star"}
input |
validWords |
Expected output |
---|---|---|
dogsflystar |
Same as above | dogs fly star |
abcabc |
Same as above | abc abc |
flystarworld |
Same as above | "" and an error |