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.
We define the lcp matrix of any 0-indexed string word of n lowercase English letters as an n x n grid such that:
lcp[i][j] is equal to the length of the longest common prefix between the substrings word[i,n-1] and word[j,n-1].Given an n x n matrix lcp, return the alphabetically smallest string word that corresponds to lcp. If there is no such string, return an empty string.
A string a is lexicographically smaller than a string b (of the same length) if in the first position where a and b differ, string a has a letter that appears earlier in the alphabet than the corresponding letter in b. For example, "aabd" is lexicographically smaller than "aaca" because the first position they differ is at the third letter, and 'b' comes before 'c'.
Example 1:
Input: lcp = [[4,0,2,0],[0,3,0,1],[2,0,2,0],[0,1,0,1]] Output: "abab" Explanation: lcp corresponds to any 4 letter string with two alternating letters. The lexicographically smallest of them is "abab".
Example 2:
Input: lcp = [[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,1]] Output: "aaaa" Explanation: lcp corresponds to any 4 letter string with a single distinct letter. The lexicographically smallest of them is "aaaa".
Example 3:
Input: lcp = [[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,3]] Output: "" Explanation: lcp[3][3] cannot be equal to 3 since word[3,...,3] consists of only a single letter; Thus, no answer exists.
Constraints:
1 <= n == lcp.length == lcp[i].length <= 10000 <= lcp[i][j] <= nProblem summary: We define the lcp matrix of any 0-indexed string word of n lowercase English letters as an n x n grid such that: lcp[i][j] is equal to the length of the longest common prefix between the substrings word[i,n-1] and word[j,n-1]. Given an n x n matrix lcp, return the alphabetically smallest string word that corresponds to lcp. If there is no such string, return an empty string. A string a is lexicographically smaller than a string b (of the same length) if in the first position where a and b differ, string a has a letter that appears earlier in the alphabet than the corresponding letter in b. For example, "aabd" is lexicographically smaller than "aaca" because the first position they differ is at the third letter, and 'b' comes before 'c'.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming · Greedy · Union-Find
[[4,0,2,0],[0,3,0,1],[2,0,2,0],[0,1,0,1]]
[[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,1]]
[[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,3]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2573: Find the String with LCP
class Solution {
public String findTheString(int[][] lcp) {
int n = lcp.length;
char[] s = new char[n];
int i = 0;
for (char c = 'a'; c <= 'z'; ++c) {
while (i < n && s[i] != '\0') {
++i;
}
if (i == n) {
break;
}
for (int j = i; j < n; ++j) {
if (lcp[i][j] > 0) {
s[j] = c;
}
}
}
for (i = 0; i < n; ++i) {
if (s[i] == '\0') {
return "";
}
}
for (i = n - 1; i >= 0; --i) {
for (int j = n - 1; j >= 0; --j) {
if (s[i] == s[j]) {
if (i == n - 1 || j == n - 1) {
if (lcp[i][j] != 1) {
return "";
}
} else if (lcp[i][j] != lcp[i + 1][j + 1] + 1) {
return "";
}
} else if (lcp[i][j] > 0) {
return "";
}
}
}
return String.valueOf(s);
}
}
// Accepted solution for LeetCode #2573: Find the String with LCP
func findTheString(lcp [][]int) string {
i, n := 0, len(lcp)
s := make([]byte, n)
for c := 'a'; c <= 'z'; c++ {
for i < n && s[i] != 0 {
i++
}
if i == n {
break
}
for j := i; j < n; j++ {
if lcp[i][j] > 0 {
s[j] = byte(c)
}
}
}
if bytes.IndexByte(s, 0) >= 0 {
return ""
}
for i := n - 1; i >= 0; i-- {
for j := n - 1; j >= 0; j-- {
if s[i] == s[j] {
if i == n-1 || j == n-1 {
if lcp[i][j] != 1 {
return ""
}
} else if lcp[i][j] != lcp[i+1][j+1]+1 {
return ""
}
} else if lcp[i][j] > 0 {
return ""
}
}
}
return string(s)
}
# Accepted solution for LeetCode #2573: Find the String with LCP
class Solution:
def findTheString(self, lcp: List[List[int]]) -> str:
n = len(lcp)
s = [""] * n
i = 0
for c in ascii_lowercase:
while i < n and s[i]:
i += 1
if i == n:
break
for j in range(i, n):
if lcp[i][j]:
s[j] = c
if "" in s:
return ""
for i in range(n - 1, -1, -1):
for j in range(n - 1, -1, -1):
if s[i] == s[j]:
if i == n - 1 or j == n - 1:
if lcp[i][j] != 1:
return ""
elif lcp[i][j] != lcp[i + 1][j + 1] + 1:
return ""
elif lcp[i][j]:
return ""
return "".join(s)
// Accepted solution for LeetCode #2573: Find the String with LCP
// 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 #2573: Find the String with LCP
// class Solution {
// public String findTheString(int[][] lcp) {
// int n = lcp.length;
// char[] s = new char[n];
// int i = 0;
// for (char c = 'a'; c <= 'z'; ++c) {
// while (i < n && s[i] != '\0') {
// ++i;
// }
// if (i == n) {
// break;
// }
// for (int j = i; j < n; ++j) {
// if (lcp[i][j] > 0) {
// s[j] = c;
// }
// }
// }
// for (i = 0; i < n; ++i) {
// if (s[i] == '\0') {
// return "";
// }
// }
// for (i = n - 1; i >= 0; --i) {
// for (int j = n - 1; j >= 0; --j) {
// if (s[i] == s[j]) {
// if (i == n - 1 || j == n - 1) {
// if (lcp[i][j] != 1) {
// return "";
// }
// } else if (lcp[i][j] != lcp[i + 1][j + 1] + 1) {
// return "";
// }
// } else if (lcp[i][j] > 0) {
// return "";
// }
// }
// }
// return String.valueOf(s);
// }
// }
// Accepted solution for LeetCode #2573: Find the String with LCP
function findTheString(lcp: number[][]): string {
let i: number = 0;
const n: number = lcp.length;
let s: string = '\0'.repeat(n);
for (let ascii = 97; ascii < 123; ++ascii) {
const c: string = String.fromCharCode(ascii);
while (i < n && s[i] !== '\0') {
++i;
}
if (i === n) {
break;
}
for (let j = i; j < n; ++j) {
if (lcp[i][j]) {
s = s.substring(0, j) + c + s.substring(j + 1);
}
}
}
if (s.indexOf('\0') !== -1) {
return '';
}
for (i = n - 1; ~i; --i) {
for (let j = n - 1; ~j; --j) {
if (s[i] === s[j]) {
if (i === n - 1 || j === n - 1) {
if (lcp[i][j] !== 1) {
return '';
}
} else if (lcp[i][j] !== lcp[i + 1][j + 1] + 1) {
return '';
}
} else if (lcp[i][j]) {
return '';
}
}
}
return s;
}
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.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.