Construct String with Minimum Cost - Problem

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 build
  • words: Array of available word building blocks
  • costs: 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

example_1.py — Basic Case
$ Input: target = "abcdef", words = ["abdef", "abc", "def"], costs = [100, 1, 1]
Output: 2
💡 Note: The optimal solution is to use "abc" (cost 1) + "def" (cost 1) = total cost 2, rather than using "abdef" alone which would cost 100.
example_2.py — Impossible Case
$ Input: target = "aaaa", words = ["a", "aa", "aaa"], costs = [1, 4, 8]
Output: 4
💡 Note: The optimal solution is to use "a" four times (4 × 1 = 4), which is cheaper than "aa" + "aa" (4 + 4 = 8) or "aaa" + "a" (8 + 1 = 9).
example_3.py — No Solution
$ Input: target = "abcd", words = ["a", "ab", "cd"], costs = [1, 2, 3]
Output: -1
💡 Note: It's impossible to construct "abcd" because we're missing a word that contains 'c' in the right position. We can make "ab" but can't continue to make "abcd".

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

Visualization

Tap to expand
Word Assembly FactoryConveyor Belt0Start1Cost: 12Cost: 2Final"abc"Cost: 1"def"Cost: 1Total Assembly Cost: 2Optimal solution: "abc" + "def" = 1 + 1 = 2
Understanding the Visualization
1
Setup Factory
Initialize conveyor belt with cost trackers at each position
2
Start Assembly
Begin with empty string (cost = 0) at position 0
3
Try Word Tiles
At each position, test all word tiles that could fit
4
Update Costs
Calculate total cost and update if it's better than current minimum
5
Final Product
The cost at the final position is our answer
Key Takeaway
🎯 Key Insight: By breaking down the problem into subproblems (minimum cost to reach each position), we can build the optimal solution incrementally, ensuring we never miss a better combination.
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
43.6K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen