Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion solution/2500-2599/2573.Find the String with LCP/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ tags:
<pre>
<strong>输入:</strong>lcp = [[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,1]]
<strong>输出:</strong>"aaaa"
<strong>解释:</strong>lcp 对应只有一个不同字母的任意 4 字母字符串,字典序最小的是 "aaaa" 。
<strong>解释:</strong>lcp 对应只有一个不同字母的任意 4 字母字符串,字典序最小的是 "aaaa" 。
</pre>

<p><strong>示例 3:</strong></p>
Expand Down Expand Up @@ -298,6 +298,56 @@ function findTheString(lcp: number[][]): string {
}
```

#### Rust

```rust
impl Solution {
pub fn find_the_string(lcp: Vec<Vec<i32>>) -> String {
let n = lcp.len();
let mut s = vec!['\0'; n];
let mut i = 0;

for c in b'a'..=b'z' {
while i < n && s[i] != '\0' {
i += 1;
}
if i == n {
break;
}
for j in i..n {
if lcp[i][j] > 0 {
s[j] = c as char;
}
}
}

for i in 0..n {
if s[i] == '\0' {
return "".to_string();
}
}

for i in (0..n).rev() {
for j in (0..n).rev() {
if s[i] == s[j] {
if i == n - 1 || j == n - 1 {
if lcp[i][j] != 1 {
return "".to_string();
}
} else if lcp[i][j] != lcp[i + 1][j + 1] + 1 {
return "".to_string();
}
} else if lcp[i][j] > 0 {
return "".to_string();
}
}
}

s.into_iter().collect()
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
52 changes: 51 additions & 1 deletion solution/2500-2599/2573.Find the String with LCP/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ tags:
<pre>
<strong>Input:</strong> lcp = [[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,1]]
<strong>Output:</strong> &quot;aaaa&quot;
<strong>Explanation:</strong> lcp corresponds to any 4 letter string with a single distinct letter. The lexicographically smallest of them is &quot;aaaa&quot;.
<strong>Explanation:</strong> lcp corresponds to any 4 letter string with a single distinct letter. The lexicographically smallest of them is &quot;aaaa&quot;.
</pre>

<p><strong class="example">Example 3:</strong></p>
Expand Down Expand Up @@ -296,6 +296,56 @@ function findTheString(lcp: number[][]): string {
}
```

#### Rust

```rust
impl Solution {
pub fn find_the_string(lcp: Vec<Vec<i32>>) -> String {
let n = lcp.len();
let mut s = vec!['\0'; n];
let mut i = 0;

for c in b'a'..=b'z' {
while i < n && s[i] != '\0' {
i += 1;
}
if i == n {
break;
}
for j in i..n {
if lcp[i][j] > 0 {
s[j] = c as char;
}
}
}

for i in 0..n {
if s[i] == '\0' {
return "".to_string();
}
}

for i in (0..n).rev() {
for j in (0..n).rev() {
if s[i] == s[j] {
if i == n - 1 || j == n - 1 {
if lcp[i][j] != 1 {
return "".to_string();
}
} else if lcp[i][j] != lcp[i + 1][j + 1] + 1 {
return "".to_string();
}
} else if lcp[i][j] > 0 {
return "".to_string();
}
}
}

s.into_iter().collect()
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
45 changes: 45 additions & 0 deletions solution/2500-2599/2573.Find the String with LCP/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
impl Solution {
pub fn find_the_string(lcp: Vec<Vec<i32>>) -> String {
let n = lcp.len();
Comment on lines +1 to +3
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR title references adding solutions for LC 2574, but this PR also adds a new Rust solution for LC 2573 and updates its documentation. Please update the PR title (or description) to reflect LC 2573 additions and the LC 2574 consolidation/renaming so the change scope is accurately represented.

Copilot uses AI. Check for mistakes.
let mut s = vec!['\0'; n];
let mut i = 0;

for c in b'a'..=b'z' {
while i < n && s[i] != '\0' {
i += 1;
}
if i == n {
break;
}
for j in i..n {
if lcp[i][j] > 0 {
s[j] = c as char;
}
}
}

for i in 0..n {
if s[i] == '\0' {
return "".to_string();
}
}

for i in (0..n).rev() {
for j in (0..n).rev() {
if s[i] == s[j] {
if i == n - 1 || j == n - 1 {
if lcp[i][j] != 1 {
return "".to_string();
}
} else if lcp[i][j] != lcp[i + 1][j + 1] + 1 {
return "".to_string();
}
} else if lcp[i][j] > 0 {
return "".to_string();
}
}
Comment on lines +27 to +40
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This validation iterates over the full n x n matrix even though the conditions are symmetric in i/j (i.e., lcp[i][j] mirrors lcp[j][i]). You can cut the work roughly in half by iterating only one triangle (e.g., j from 0..=i) while keeping the same checks, which helps if n is large.

Copilot uses AI. Check for mistakes.
}

s.into_iter().collect()
}
}
Loading
Loading