Imagine you're building words from a collection of building blocks! You have an array of strings called words and a target string you want to construct.
Here's the twist: you can only use prefixes of the words in your collection. A string x is valid if it matches the beginning of any word in words.
Your mission: Find the minimum number of valid strings that can be concatenated together to form the exact target string. If it's impossible to construct the target, return -1.
Example: If words = ["abc", "aaaaa", "bcdef"] and target = "aabcdabc", you could use prefixes like "aa" (from "aaaaa"), "bcd" (from "bcdef"), and "abc" (from "abc") to build your target efficiently!
Input & Output
Time & Space Complexity
In worst case, we might try all possible ways to split the target string, leading to exponential combinations
Recursion stack depth can go up to the length of target string
Constraints
- 1 โค words.length โค 100
- 1 โค words[i].length โค 5 ร 103
- 1 โค target.length โค 5 ร 103
- words[i] and target consist only of lowercase English letters
- The input is guaranteed that at least one valid prefix exists for each character in target