Skip to content

Instantly share code, notes, and snippets.

@m-esm
Created May 17, 2022 10:48
Show Gist options
  • Save m-esm/e01e086a0991882abb90559693b1a16b to your computer and use it in GitHub Desktop.
Save m-esm/e01e086a0991882abb90559693b1a16b to your computer and use it in GitHub Desktop.
Whitespace coding challenge

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)

Example

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment