Find the String with LCP - Problem
Reverse Engineering Challenge: Reconstruct a String from its LCP Matrix

Imagine you're given a mysterious Longest Common Prefix (LCP) matrix and need to reverse-engineer the original string that created it. This is exactly what this problem asks you to do!

Given an n x n matrix lcp, where lcp[i][j] represents the length of the longest common prefix between suffixes starting at positions i and j, you must find the lexicographically smallest string that could have generated this matrix.

Key Points:
• The LCP matrix is derived from suffixes: word[i...] and word[j...]
• Return the alphabetically smallest valid string
• If no valid string exists, return an empty string

Example: If word = "aba", then:
• Suffixes: ["aba", "ba", "a"]
lcp[0][1] = 1 ("aba" vs "ba" → common prefix "a")
lcp[1][2] = 0 ("ba" vs "a" → no common prefix)

Input & Output

example_1.py — Simple Pattern
$ Input: lcp = [[4,0,2,0],[0,3,0,1],[2,0,2,0],[0,1,0,1]]
Output: "abab"
💡 Note: The LCP matrix indicates that positions 0,2 must have the same character (lcp[0][2]=2) and positions 1,3 must have the same character (lcp[1][3]=1). The lexicographically smallest assignment is 'a' for positions 0,2 and 'b' for positions 1,3, giving us "abab".
example_2.py — All Same Characters
$ Input: lcp = [[2,1],[1,1]]
Output: "aa"
💡 Note: Since lcp[0][1] = 1 > 0, positions 0 and 1 must have the same character. The lexicographically smallest choice is 'a', resulting in "aa". We can verify: lcp[0][0] = 2 ✓, lcp[1][1] = 1 ✓, lcp[0][1] = lcp[1][0] = 1 ✓
example_3.py — Invalid Matrix
$ Input: lcp = [[1,1],[1,1]]
Output: ""
💡 Note: This matrix is invalid because lcp[0][0] should be 2 (length from position 0 to end), but it's given as 1. Since no valid string can produce this LCP matrix, we return an empty string.

Constraints

  • n == lcp.length == lcp[i].length
  • 1 ≤ n ≤ 1000
  • 0 ≤ lcp[i][j] ≤ n
  • The input matrix may not represent a valid LCP matrix
  • If multiple valid strings exist, return the lexicographically smallest one

Visualization

Tap to expand
🧬 LCP Matrix → String ReconstructionInput LCP Matrix4020030120200101Union-Find Groups0123Same char groupsCharacter AssignmentGroup 1 → 'a'Positions 0, 2Group 2 → 'b'Positions 1, 3Result: "abab"Lexicographically smallest valid string🔍 Key Properties:• lcp[i][j] > 0 ⟹ word[i] = word[j] • Matrix is symmetric • lcp[i][i] = n-i • Assign chars greedily: 'a', 'b', 'c'...
Understanding the Visualization
1
Matrix Validation
Check if the LCP matrix follows required mathematical properties
2
Position Grouping
Use Union-Find to group positions that must have identical characters
3
Character Assignment
Assign lexicographically smallest characters to each group
4
Result Verification
Validate that our constructed string produces the input LCP matrix
Key Takeaway
🎯 Key Insight: LCP matrices encode character equality constraints - if two suffixes share a common prefix, their starting positions must have the same character. Union-Find efficiently groups these constraints, allowing us to build the lexicographically smallest valid string!
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
43.4K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen