Class Solution
- java.lang.Object
-
- g2501_2600.s2573_find_the_string_with_lcp.Solution
-
public class Solution extends Object
2573 - Find the String with LCP.Hard
We define the
lcpmatrix of any 0-indexed stringwordofnlowercase English letters as ann x ngrid such that:lcp[i][j]is equal to the length of the longest common prefix between the substringsword[i,n-1]andword[j,n-1].
Given an
n x nmatrixlcp, return the alphabetically smallest stringwordthat corresponds tolcp. If there is no such string, return an empty string.A string
ais lexicographically smaller than a stringb(of the same length) if in the first position whereaandbdiffer, stringahas a letter that appears earlier in the alphabet than the corresponding letter inb. 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] <= n
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
-
-
Method Detail
-
findTheString
public String findTheString(int[][] lcp)
-
-