Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given a string target, an array of strings words, and an integer array costs, both arrays of the same length.
Imagine an empty string s.
You can perform the following operation any number of times (including zero):
i in the range [0, words.length - 1].words[i] to s.costs[i].Return the minimum cost to make s equal to target. If it's not possible, return -1.
Example 1:
Input: target = "abcdef", words = ["abdef","abc","d","def","ef"], costs = [100,1,1,10,5]
Output: 7
Explanation:
The minimum cost can be achieved by performing the following operations:
"abc" to s at a cost of 1, resulting in s = "abc"."d" to s at a cost of 1, resulting in s = "abcd"."ef" to s at a cost of 5, resulting in s = "abcdef".Example 2:
Input: target = "aaaa", words = ["z","zz","zzz"], costs = [1,10,100]
Output: -1
Explanation:
It is impossible to make s equal to target, so we return -1.
Constraints:
1 <= target.length <= 5 * 1041 <= words.length == costs.length <= 5 * 1041 <= words[i].length <= target.lengthwords[i].length is less than or equal to 5 * 104.target and words[i] consist only of lowercase English letters.1 <= costs[i] <= 104Problem summary: You are given a string target, an array of strings words, and an integer array costs, both arrays of the same length. Imagine an empty string s. You can perform the following operation any number of times (including zero): Choose an index i in the range [0, words.length - 1]. Append words[i] to s. The cost of operation is costs[i]. Return the minimum cost to make s equal to target. If it's not possible, return -1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
"abcdef" ["abdef","abc","d","def","ef"] [100,1,1,10,5]
"aaaa" ["z","zz","zzz"] [1,10,100]
minimum-number-of-valid-strings-to-form-target-ii)minimum-number-of-valid-strings-to-form-target-i)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3213: Construct String with Minimum Cost
class Hashing {
private final long[] p;
private final long[] h;
private final long mod;
public Hashing(String word, long base, int mod) {
int n = word.length();
p = new long[n + 1];
h = new long[n + 1];
p[0] = 1;
this.mod = mod;
for (int i = 1; i <= n; i++) {
p[i] = p[i - 1] * base % mod;
h[i] = (h[i - 1] * base + word.charAt(i - 1)) % mod;
}
}
public long query(int l, int r) {
return (h[r] - h[l - 1] * p[r - l + 1] % mod + mod) % mod;
}
}
class Solution {
public int minimumCost(String target, String[] words, int[] costs) {
final int base = 13331;
final int mod = 998244353;
final int inf = Integer.MAX_VALUE / 2;
int n = target.length();
Hashing hashing = new Hashing(target, base, mod);
int[] f = new int[n + 1];
Arrays.fill(f, inf);
f[0] = 0;
TreeSet<Integer> ss = new TreeSet<>();
for (String w : words) {
ss.add(w.length());
}
Map<Long, Integer> d = new HashMap<>();
for (int i = 0; i < words.length; i++) {
long x = 0;
for (char c : words[i].toCharArray()) {
x = (x * base + c) % mod;
}
d.merge(x, costs[i], Integer::min);
}
for (int i = 1; i <= n; i++) {
for (int j : ss) {
if (j > i) {
break;
}
long x = hashing.query(i - j + 1, i);
f[i] = Math.min(f[i], f[i - j] + d.getOrDefault(x, inf));
}
}
return f[n] >= inf ? -1 : f[n];
}
}
// Accepted solution for LeetCode #3213: Construct String with Minimum Cost
type Hashing struct {
p []int64
h []int64
mod int64
}
func NewHashing(word string, base, mod int64) *Hashing {
n := len(word)
p := make([]int64, n+1)
h := make([]int64, n+1)
p[0] = 1
for i := 1; i <= n; i++ {
p[i] = p[i-1] * base % mod
h[i] = (h[i-1]*base + int64(word[i-1])) % mod
}
return &Hashing{p, h, mod}
}
func (hs *Hashing) query(l, r int) int64 {
return (hs.h[r] - hs.h[l-1]*hs.p[r-l+1]%hs.mod + hs.mod) % hs.mod
}
func minimumCost(target string, words []string, costs []int) int {
const base = 13331
const mod = 998244353
const inf = math.MaxInt32 / 2
n := len(target)
hashing := NewHashing(target, base, mod)
f := make([]int, n+1)
for i := range f {
f[i] = inf
}
f[0] = 0
ss := make(map[int]struct{})
for _, w := range words {
ss[len(w)] = struct{}{}
}
lengths := make([]int, 0, len(ss))
for length := range ss {
lengths = append(lengths, length)
}
sort.Ints(lengths)
d := make(map[int64]int)
for i, w := range words {
var x int64
for _, c := range w {
x = (x*base + int64(c)) % mod
}
if existingCost, exists := d[x]; exists {
if costs[i] < existingCost {
d[x] = costs[i]
}
} else {
d[x] = costs[i]
}
}
for i := 1; i <= n; i++ {
for _, j := range lengths {
if j > i {
break
}
x := hashing.query(i-j+1, i)
if cost, ok := d[x]; ok {
f[i] = min(f[i], f[i-j]+cost)
}
}
}
if f[n] >= inf {
return -1
}
return f[n]
}
# Accepted solution for LeetCode #3213: Construct String with Minimum Cost
class Solution:
def minimumCost(self, target: str, words: List[str], costs: List[int]) -> int:
base, mod = 13331, 998244353
n = len(target)
h = [0] * (n + 1)
p = [1] * (n + 1)
for i, c in enumerate(target, 1):
h[i] = (h[i - 1] * base + ord(c)) % mod
p[i] = (p[i - 1] * base) % mod
f = [0] + [inf] * n
ss = sorted(set(map(len, words)))
d = defaultdict(lambda: inf)
min = lambda a, b: a if a < b else b
for w, c in zip(words, costs):
x = 0
for ch in w:
x = (x * base + ord(ch)) % mod
d[x] = min(d[x], c)
for i in range(1, n + 1):
for j in ss:
if j > i:
break
x = (h[i] - h[i - j] * p[j]) % mod
f[i] = min(f[i], f[i - j] + d[x])
return f[n] if f[n] < inf else -1
// Accepted solution for LeetCode #3213: Construct String with Minimum Cost
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3213: Construct String with Minimum Cost
// class Hashing {
// private final long[] p;
// private final long[] h;
// private final long mod;
//
// public Hashing(String word, long base, int mod) {
// int n = word.length();
// p = new long[n + 1];
// h = new long[n + 1];
// p[0] = 1;
// this.mod = mod;
// for (int i = 1; i <= n; i++) {
// p[i] = p[i - 1] * base % mod;
// h[i] = (h[i - 1] * base + word.charAt(i - 1)) % mod;
// }
// }
//
// public long query(int l, int r) {
// return (h[r] - h[l - 1] * p[r - l + 1] % mod + mod) % mod;
// }
// }
//
// class Solution {
// public int minimumCost(String target, String[] words, int[] costs) {
// final int base = 13331;
// final int mod = 998244353;
// final int inf = Integer.MAX_VALUE / 2;
//
// int n = target.length();
// Hashing hashing = new Hashing(target, base, mod);
//
// int[] f = new int[n + 1];
// Arrays.fill(f, inf);
// f[0] = 0;
//
// TreeSet<Integer> ss = new TreeSet<>();
// for (String w : words) {
// ss.add(w.length());
// }
//
// Map<Long, Integer> d = new HashMap<>();
// for (int i = 0; i < words.length; i++) {
// long x = 0;
// for (char c : words[i].toCharArray()) {
// x = (x * base + c) % mod;
// }
// d.merge(x, costs[i], Integer::min);
// }
//
// for (int i = 1; i <= n; i++) {
// for (int j : ss) {
// if (j > i) {
// break;
// }
// long x = hashing.query(i - j + 1, i);
// f[i] = Math.min(f[i], f[i - j] + d.getOrDefault(x, inf));
// }
// }
//
// return f[n] >= inf ? -1 : f[n];
// }
// }
// Accepted solution for LeetCode #3213: Construct String with Minimum Cost
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #3213: Construct String with Minimum Cost
// class Hashing {
// private final long[] p;
// private final long[] h;
// private final long mod;
//
// public Hashing(String word, long base, int mod) {
// int n = word.length();
// p = new long[n + 1];
// h = new long[n + 1];
// p[0] = 1;
// this.mod = mod;
// for (int i = 1; i <= n; i++) {
// p[i] = p[i - 1] * base % mod;
// h[i] = (h[i - 1] * base + word.charAt(i - 1)) % mod;
// }
// }
//
// public long query(int l, int r) {
// return (h[r] - h[l - 1] * p[r - l + 1] % mod + mod) % mod;
// }
// }
//
// class Solution {
// public int minimumCost(String target, String[] words, int[] costs) {
// final int base = 13331;
// final int mod = 998244353;
// final int inf = Integer.MAX_VALUE / 2;
//
// int n = target.length();
// Hashing hashing = new Hashing(target, base, mod);
//
// int[] f = new int[n + 1];
// Arrays.fill(f, inf);
// f[0] = 0;
//
// TreeSet<Integer> ss = new TreeSet<>();
// for (String w : words) {
// ss.add(w.length());
// }
//
// Map<Long, Integer> d = new HashMap<>();
// for (int i = 0; i < words.length; i++) {
// long x = 0;
// for (char c : words[i].toCharArray()) {
// x = (x * base + c) % mod;
// }
// d.merge(x, costs[i], Integer::min);
// }
//
// for (int i = 1; i <= n; i++) {
// for (int j : ss) {
// if (j > i) {
// break;
// }
// long x = hashing.query(i - j + 1, i);
// f[i] = Math.min(f[i], f[i - j] + d.getOrDefault(x, inf));
// }
// }
//
// return f[n] >= inf ? -1 : f[n];
// }
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.