Phrase maximization
Imagine we have the following sentence.
she sells sea shells by the sea shore
We want phrases that are as big as possible, yet no bigger than 10 characters long. A phrase must contain entire words and a space between them. And an instance of a word should only exist in the first phrase that matches.
We start at the beginning. "she sells" is <= 10 characters. We can't add the following "sea" because "she sells sea" is 13 characters. That means "she sells" is a maxmizing phrase. We now start at "sea" and continue.
Your job is to write a function that takes a sentence and outputs a collection of phrases.
Examples
(phrases 10 "she sells sea shells by the sea shore") ;=> ["she sells" "sea shells" "by the sea" "shore"]
(phrases 2 "she sells sea shells by the sea shore") ;=> ["by"]
(phrases 2 "the big dog jumped over the fence") ;=> []
(phrases 13 "she sells sea shells by the sea shore") ;=> ["she sells sea" "shells by the" "sea shore"]
Notes
- Trim initial and trailing whitespace, but leave whitespace between words.
- The spaces count in the length of the phrases.
Thanks to this site for the problem idea, where it is rated Expert in Java. The problem has been modified.
Please submit your solutions as comments on this gist.
To subscribe: https://purelyfunctional.tv/newsletter/