vocab and a string target. A piece is any non-empty string that is a beginning (prefix) of at least one word in vocab; a whole word counts too.
Glue target together from pieces, using as few as possible (the same piece may be used many times). Return the smallest number of pieces, or -1 if target cannot be built.vocab=["sun", "flower"], target="sunflow" → 2
sun is a start of sun and flow is a start of flower: two pieces.
vocab=["abc", "b"], target="abab" → 2
ab + ab: both pieces are a start of abc.
vocab=["cat"], target="dog" → -1
No word starts with d.
vocab is at most 10⁵vocab=["sun", "flower"], target="sunflow"
2
Run checks the first 3 tests in your browser. Submit checks all 152 tests on the server, hidden ones included.