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
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
Example: If target =
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
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
ā Quadratic Growth
Space Complexity
O(n)
Memoization table stores results for each position in target string
ā” 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
š”
Explanation
AI Ready
š” Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code