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.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 0-indexed array words containing n strings.
Let's define a join operation join(x, y) between two strings x and y as concatenating them into xy. However, if the last character of x is equal to the first character of y, one of them is deleted.
For example join("ab", "ba") = "aba" and join("ab", "cde") = "abcde".
You are to perform n - 1 join operations. Let str0 = words[0]. Starting from i = 1 up to i = n - 1, for the ith operation, you can do one of the following:
stri = join(stri - 1, words[i])stri = join(words[i], stri - 1)Your task is to minimize the length of strn - 1.
Return an integer denoting the minimum possible length of strn - 1.
Example 1:
Input: words = ["aa","ab","bc"] Output: 4 Explanation: In this example, we can perform join operations in the following order to minimize the length of str2: str0 = "aa" str1 = join(str0, "ab") = "aab" str2 = join(str1, "bc") = "aabc" It can be shown that the minimum possible length of str2 is 4.
Example 2:
Input: words = ["ab","b"]
Output: 2
Explanation: In this example, str0 = "ab", there are two ways to get str1:
join(str0, "b") = "ab" or join("b", str0) = "bab".
The first string, "ab", has the minimum length. Hence, the answer is 2.
Example 3:
Input: words = ["aaa","c","aba"]
Output: 6
Explanation: In this example, we can perform join operations in the following order to minimize the length of str2:
str0 = "aaa"
str1 = join(str0, "c") = "aaac"
str2 = join("aba", str1) = "abaaac"
It can be shown that the minimum possible length of str2 is 6.
Constraints:
1 <= words.length <= 10001 <= words[i].length <= 50words[i] is an English lowercase letterProblem summary: You are given a 0-indexed array words containing n strings. Let's define a join operation join(x, y) between two strings x and y as concatenating them into xy. However, if the last character of x is equal to the first character of y, one of them is deleted. For example join("ab", "ba") = "aba" and join("ab", "cde") = "abcde". You are to perform n - 1 join operations. Let str0 = words[0]. Starting from i = 1 up to i = n - 1, for the ith operation, you can do one of the following: Make stri = join(stri - 1, words[i]) Make stri = join(words[i], stri - 1) Your task is to minimize the length of strn - 1. Return an integer denoting the minimum possible length of strn - 1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
["aa","ab","bc"]
["ab","b"]
["aaa","c","aba"]
largest-merge-of-two-strings)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2746: Decremental String Concatenation
class Solution {
private Integer[][][] f;
private String[] words;
private int n;
public int minimizeConcatenatedLength(String[] words) {
n = words.length;
this.words = words;
f = new Integer[n][26][26];
return words[0].length()
+ dfs(1, words[0].charAt(0) - 'a', words[0].charAt(words[0].length() - 1) - 'a');
}
private int dfs(int i, int a, int b) {
if (i >= n) {
return 0;
}
if (f[i][a][b] != null) {
return f[i][a][b];
}
String s = words[i];
int m = s.length();
int x = dfs(i + 1, a, s.charAt(m - 1) - 'a') - (s.charAt(0) - 'a' == b ? 1 : 0);
int y = dfs(i + 1, s.charAt(0) - 'a', b) - (s.charAt(m - 1) - 'a' == a ? 1 : 0);
return f[i][a][b] = m + Math.min(x, y);
}
}
// Accepted solution for LeetCode #2746: Decremental String Concatenation
func minimizeConcatenatedLength(words []string) int {
n := len(words)
f := make([][26][26]int, n)
var dfs func(i, a, b int) int
dfs = func(i, a, b int) int {
if i >= n {
return 0
}
if f[i][a][b] > 0 {
return f[i][a][b]
}
s := words[i]
m := len(s)
x := dfs(i+1, a, int(s[m-1]-'a'))
y := dfs(i+1, int(s[0]-'a'), b)
if int(s[0]-'a') == b {
x--
}
if int(s[m-1]-'a') == a {
y--
}
f[i][a][b] = m + min(x, y)
return f[i][a][b]
}
return len(words[0]) + dfs(1, int(words[0][0]-'a'), int(words[0][len(words[0])-1]-'a'))
}
# Accepted solution for LeetCode #2746: Decremental String Concatenation
class Solution:
def minimizeConcatenatedLength(self, words: List[str]) -> int:
@cache
def dfs(i: int, a: str, b: str) -> int:
if i >= len(words):
return 0
s = words[i]
x = dfs(i + 1, a, s[-1]) - int(s[0] == b)
y = dfs(i + 1, s[0], b) - int(s[-1] == a)
return len(s) + min(x, y)
return len(words[0]) + dfs(1, words[0][0], words[0][-1])
// Accepted solution for LeetCode #2746: Decremental String Concatenation
// 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 #2746: Decremental String Concatenation
// class Solution {
// private Integer[][][] f;
// private String[] words;
// private int n;
//
// public int minimizeConcatenatedLength(String[] words) {
// n = words.length;
// this.words = words;
// f = new Integer[n][26][26];
// return words[0].length()
// + dfs(1, words[0].charAt(0) - 'a', words[0].charAt(words[0].length() - 1) - 'a');
// }
//
// private int dfs(int i, int a, int b) {
// if (i >= n) {
// return 0;
// }
// if (f[i][a][b] != null) {
// return f[i][a][b];
// }
// String s = words[i];
// int m = s.length();
// int x = dfs(i + 1, a, s.charAt(m - 1) - 'a') - (s.charAt(0) - 'a' == b ? 1 : 0);
// int y = dfs(i + 1, s.charAt(0) - 'a', b) - (s.charAt(m - 1) - 'a' == a ? 1 : 0);
// return f[i][a][b] = m + Math.min(x, y);
// }
// }
// Accepted solution for LeetCode #2746: Decremental String Concatenation
function minimizeConcatenatedLength(words: string[]): number {
const n = words.length;
const f: number[][][] = Array(n)
.fill(0)
.map(() =>
Array(26)
.fill(0)
.map(() => Array(26).fill(0)),
);
const dfs = (i: number, a: number, b: number): number => {
if (i >= n) {
return 0;
}
if (f[i][a][b] > 0) {
return f[i][a][b];
}
const s = words[i];
const m = s.length;
const x =
dfs(i + 1, a, s[m - 1].charCodeAt(0) - 97) - (s[0].charCodeAt(0) - 97 === b ? 1 : 0);
const y =
dfs(i + 1, s[0].charCodeAt(0) - 97, b) - (s[m - 1].charCodeAt(0) - 97 === a ? 1 : 0);
return (f[i][a][b] = Math.min(x + m, y + m));
};
return (
words[0].length +
dfs(1, words[0][0].charCodeAt(0) - 97, words[0][words[0].length - 1].charCodeAt(0) - 97)
);
}
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.