You're tasked with building a target string using a collection of word building blocks, where each block has an associated cost. Think of it like constructing a sentence using pre-made word cards, but you want to spend the least amount of money possible!
The Challenge: Given a target string, an array of available words, and their corresponding costs, find the minimum cost to construct the target string by concatenating words from your collection. You can use each word multiple times, and the order matters!
Input:
target: The string you need to buildwords: Array of available word building blockscosts: Array of costs for each corresponding word
Output: Return the minimum cost to construct the target string, or -1 if it's impossible.
Example: If target = "abcdef", words = ["abdef", "abc", "def"], and costs = [100, 1, 1], you could use "abc" + "def" for a total cost of 2, rather than using "abdef" alone which would cost 100.
Input & Output
Constraints
- 1 ≤ target.length ≤ 5 × 104
- 1 ≤ words.length ≤ 1000
- 1 ≤ words[i].length ≤ 50
- 1 ≤ costs[i] ≤ 104
- target and words[i] consist only of lowercase English letters
- words.length == costs.length