2573. Find the String with LCP Hard
1/**
2 * [2573] Find the String with LCP
3 *
4 * We define the lcp matrix of any 0-indexed string word of n lowercase English letters as an n x n grid such that:
5 *
6 * 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].
7 *
8 * 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.
9 * 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'.
10 *
11 * <strong class="example">Example 1:
12 *
13 * Input: lcp = [[4,0,2,0],[0,3,0,1],[2,0,2,0],[0,1,0,1]]
14 * Output: "abab"
15 * Explanation: lcp corresponds to any 4 letter string with two alternating letters. The lexicographically smallest of them is "abab".
16 *
17 * <strong class="example">Example 2:
18 *
19 * Input: lcp = [[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,1]]
20 * Output: "aaaa"
21 * Explanation: lcp corresponds to any 4 letter string with a single distinct letter. The lexicographically smallest of them is "aaaa".
22 *
23 * <strong class="example">Example 3:
24 *
25 * Input: lcp = [[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,3]]
26 * Output: ""
27 * Explanation: lcp[3][3] cannot be equal to 3 since word[3,...,3] consists of only a single letter; Thus, no answer exists.
28 *
29 *
30 * Constraints:
31 *
32 * 1 <= n == lcp.length == lcp[i].length <= 1000
33 * <font face="monospace">0 <= lcp[i][j] <= n</font>
34 *
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/find-the-string-with-lcp/
39// discuss: https://leetcode.com/problems/find-the-string-with-lcp/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44 pub fn find_the_string(lcp: Vec<Vec<i32>>) -> String {
45 String::new()
46 }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn test_2573() {
57 }
58}
59
Back
© 2025 bowen.ge All Rights Reserved.