Construct String with Minimum Cost (Easy) - Problem
Build Your Dream String with Minimum Cost!

You're running a string construction service where customers can build custom strings by purchasing word components. You have a target string that a customer wants, and you have an inventory of words with their respective costs.

Your job is to find the minimum cost to construct the exact target string by concatenating words from your inventory. You can use each word multiple times, and the order matters - you're building the string from left to right.

Goal: Return the minimum cost to build the target string, or -1 if it's impossible.

Example: If target = "abcdef", words = ["abcdef", "ab", "cd", "ef"], costs = [100, 1, 1, 1], you could use the expensive single word (cost 100) or combine cheaper parts: "ab" + "cd" + "ef" (cost 3).

Input & Output

example_1.py — Basic Word Construction
$ Input: target = "abcdef", words = ["abcdef", "ab", "cd", "def"], costs = [100, 1, 1, 1]
› Output: 3
šŸ’” Note: We can construct "abcdef" using "ab" (cost 1) + "cd" (cost 1) + "def" (cost 1) = total cost 3, which is cheaper than using the single word "abcdef" (cost 100).
example_2.py — Impossible Construction
$ Input: target = "aaaa", words = ["a", "aa", "aaa"], costs = [1, 2, 3]
› Output: 4
šŸ’” Note: The optimal way is to use four "a" words: "a" + "a" + "a" + "a" = cost 1+1+1+1 = 4. Using "aa" + "aa" would cost 2+2 = 4, and "aaa" + "a" would cost 3+1 = 4.
example_3.py — No Valid Construction
$ Input: target = "abcd", words = ["ab", "cd", "ef"], costs = [1, 1, 1]
› Output: -1
šŸ’” Note: We can form "ab" + "cd" = "abcd", so the minimum cost is 1 + 1 = 2. Wait, this should return 2, not -1. Let me correct: if target = "abcde" and we only have ["ab", "cd"], then it's impossible to form "abcde", returning -1.

Visualization

Tap to expand
String Construction with Minimum CostTarget: "abcdef"Words: ["ab", "cd", "ef", "abcdef"] Costs: [1, 1, 1, 100]Solution Approaches:Single Word"abcdef" = 100Multiple Words"ab"+"cd"+"ef" = 3OPTIMAL!DP ProcessTry all combinationsDynamic Programming finds the minimum cost path through all possibilitiesšŸŽÆ Key Insight: At each position, try all valid words and take minimum costMemoization ensures each subproblem is solved only once
Understanding the Visualization
1
Identify Goal
We need to construct target string 'abcdef' with minimum cost
2
Try Word Placements
At each position, try all words that can fit and match
3
Calculate Costs
For each valid placement, add word cost to remaining string cost
4
Choose Minimum
Select the combination that gives minimum total cost
Key Takeaway
šŸŽÆ Key Insight: Dynamic Programming with memoization transforms an exponential problem into polynomial time by storing and reusing solutions to subproblems.

Time & Space Complexity

Time Complexity
ā±ļø
O(n²m)

Where n is target length and m is number of words. Each position is computed once, and we check all words for each position

n
2n
⚠ Quadratic Growth
Space Complexity
O(n)

Memoization table stores results for each position in target string

n
2n
⚔ Linearithmic Space

Constraints

  • 1 ≤ target.length ≤ 1000
  • 1 ≤ words.length ≤ 100
  • 1 ≤ words[i].length ≤ 1000
  • 1 ≤ costs[i] ≤ 104
  • All strings consist of lowercase English letters only
  • costs.length == words.length
Asked in
Google 42 Amazon 38 Meta 35 Microsoft 29
28.5K Views
High Frequency
~18 min Avg. Time
892 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