Skip to content

Practice Construct String with Minimum Cost

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3213
LeetCode Problem

Construct String with Minimum Cost

Problem Statement

Starting with an empty string, repeatedly choose an index i and append words[i], paying costs[i] each time. Indices may be reused. Given target and equally sized words and costs arrays, return the minimum cost of constructing exactly target, or -1 if it cannot be formed.

Source: Construct String with Minimum Cost on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
target = "abcdef", words = ["abdef","abc","d","def","ef"], costs = [100,1,1,10,5]
Output
7

# Example 2

Input
target = "aaaa", words = ["z","zz","zzz"], costs = [1,10,100]
Output
-1

Constraints

1 <= target.length <= 5 * 10^4
1 <= words.length == costs.length <= 5 * 10^4
1 <= words[i].length <= target.length
The total sum of words[i].length is less than or equal to 5 * 10^4.
target and words[i] consist only of lowercase English letters.
1 <= costs[i] <= 10^4

Related LeetCode Problems

Practice more problems similar to Construct String with Minimum Cost.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.