Skip to content

Commit 0a02a4b

Browse files
committed
feat: add solutions for lc No.2574
1 parent 2155e1d commit 0a02a4b

15 files changed

Lines changed: 343 additions & 379 deletions

File tree

solution/2500-2599/2573.Find the String with LCP/README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ tags:
4848
<pre>
4949
<strong>输入:</strong>lcp = [[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,1]]
5050
<strong>输出:</strong>"aaaa"
51-
<strong>解释:</strong>lcp 对应只有一个不同字母的任意 4 字母字符串,字典序最小的是 "aaaa" 。
51+
<strong>解释:</strong>lcp 对应只有一个不同字母的任意 4 字母字符串,字典序最小的是 "aaaa" 。
5252
</pre>
5353

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

301+
#### Rust
302+
303+
```rust
304+
impl Solution {
305+
pub fn find_the_string(lcp: Vec<Vec<i32>>) -> String {
306+
let n = lcp.len();
307+
let mut s = vec!['\0'; n];
308+
let mut i = 0;
309+
310+
for c in b'a'..=b'z' {
311+
while i < n && s[i] != '\0' {
312+
i += 1;
313+
}
314+
if i == n {
315+
break;
316+
}
317+
for j in i..n {
318+
if lcp[i][j] > 0 {
319+
s[j] = c as char;
320+
}
321+
}
322+
}
323+
324+
for i in 0..n {
325+
if s[i] == '\0' {
326+
return "".to_string();
327+
}
328+
}
329+
330+
for i in (0..n).rev() {
331+
for j in (0..n).rev() {
332+
if s[i] == s[j] {
333+
if i == n - 1 || j == n - 1 {
334+
if lcp[i][j] != 1 {
335+
return "".to_string();
336+
}
337+
} else if lcp[i][j] != lcp[i + 1][j + 1] + 1 {
338+
return "".to_string();
339+
}
340+
} else if lcp[i][j] > 0 {
341+
return "".to_string();
342+
}
343+
}
344+
}
345+
346+
s.into_iter().collect()
347+
}
348+
}
349+
```
350+
301351
<!-- tabs:end -->
302352

303353
<!-- solution:end -->

solution/2500-2599/2573.Find the String with LCP/README_EN.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ tags:
4747
<pre>
4848
<strong>Input:</strong> lcp = [[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,1]]
4949
<strong>Output:</strong> &quot;aaaa&quot;
50-
<strong>Explanation:</strong> lcp corresponds to any 4 letter string with a single distinct letter. The lexicographically smallest of them is &quot;aaaa&quot;.
50+
<strong>Explanation:</strong> lcp corresponds to any 4 letter string with a single distinct letter. The lexicographically smallest of them is &quot;aaaa&quot;.
5151
</pre>
5252

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

299+
#### Rust
300+
301+
```rust
302+
impl Solution {
303+
pub fn find_the_string(lcp: Vec<Vec<i32>>) -> String {
304+
let n = lcp.len();
305+
let mut s = vec!['\0'; n];
306+
let mut i = 0;
307+
308+
for c in b'a'..=b'z' {
309+
while i < n && s[i] != '\0' {
310+
i += 1;
311+
}
312+
if i == n {
313+
break;
314+
}
315+
for j in i..n {
316+
if lcp[i][j] > 0 {
317+
s[j] = c as char;
318+
}
319+
}
320+
}
321+
322+
for i in 0..n {
323+
if s[i] == '\0' {
324+
return "".to_string();
325+
}
326+
}
327+
328+
for i in (0..n).rev() {
329+
for j in (0..n).rev() {
330+
if s[i] == s[j] {
331+
if i == n - 1 || j == n - 1 {
332+
if lcp[i][j] != 1 {
333+
return "".to_string();
334+
}
335+
} else if lcp[i][j] != lcp[i + 1][j + 1] + 1 {
336+
return "".to_string();
337+
}
338+
} else if lcp[i][j] > 0 {
339+
return "".to_string();
340+
}
341+
}
342+
}
343+
344+
s.into_iter().collect()
345+
}
346+
}
347+
```
348+
299349
<!-- tabs:end -->
300350

301351
<!-- solution:end -->
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
impl Solution {
2+
pub fn find_the_string(lcp: Vec<Vec<i32>>) -> String {
3+
let n = lcp.len();
4+
let mut s = vec!['\0'; n];
5+
let mut i = 0;
6+
7+
for c in b'a'..=b'z' {
8+
while i < n && s[i] != '\0' {
9+
i += 1;
10+
}
11+
if i == n {
12+
break;
13+
}
14+
for j in i..n {
15+
if lcp[i][j] > 0 {
16+
s[j] = c as char;
17+
}
18+
}
19+
}
20+
21+
for i in 0..n {
22+
if s[i] == '\0' {
23+
return "".to_string();
24+
}
25+
}
26+
27+
for i in (0..n).rev() {
28+
for j in (0..n).rev() {
29+
if s[i] == s[j] {
30+
if i == n - 1 || j == n - 1 {
31+
if lcp[i][j] != 1 {
32+
return "".to_string();
33+
}
34+
} else if lcp[i][j] != lcp[i + 1][j + 1] + 1 {
35+
return "".to_string();
36+
}
37+
} else if lcp[i][j] > 0 {
38+
return "".to_string();
39+
}
40+
}
41+
}
42+
43+
s.into_iter().collect()
44+
}
45+
}

0 commit comments

Comments
 (0)