Skip to content

Commit a12260e

Browse files
authoredAug 25, 2023
feat: update rust solution to lc problem: No.0005 (doocs#1498)
No.0005.Longest Palindromic Substring
1 parent 652d72b commit a12260e

File tree

3 files changed

+89
-55
lines changed

3 files changed

+89
-55
lines changed
 

‎solution/0000-0099/0005.Longest Palindromic Substring/README.md

+38-17
Original file line numberDiff line numberDiff line change
@@ -362,30 +362,51 @@ function longestPalindrome(s: string): string {
362362
```rust
363363
impl Solution {
364364
pub fn longest_palindrome(s: String) -> String {
365-
let n = s.len();
366-
let s = s.as_bytes();
367-
let is_pass = |mut l, mut r| {
368-
while l < r {
369-
if s[l] != s[r] {
370-
return false;
365+
let (n, mut ans) = (s.len(), &s[..1]);
366+
let mut dp = vec![vec![false; n]; n];
367+
let data: Vec<char> = s.chars().collect();
368+
369+
for end in 1..n {
370+
for start in 0..=end {
371+
if data[start] == data[end] {
372+
dp[start][end] = end - start < 2 || dp[start + 1][end - 1];
373+
if dp[start][end] && (end - start + 1) > ans.len() {
374+
ans = &s[start..=end];
375+
}
371376
}
372-
l += 1;
373-
r -= 1;
374377
}
375-
true
376-
};
377-
let mut res = &s[0..1];
378-
for i in 0..n - 1 {
379-
for j in (i + 1..n).rev() {
380-
if res.len() > j - i {
378+
}
379+
ans.to_string()
380+
}
381+
}
382+
```
383+
384+
```rust
385+
impl Solution {
386+
pub fn is_palindrome(s: &str) -> bool {
387+
let mut chars = s.chars();
388+
while let (Some(c1), Some(c2)) = (chars.next(), chars.next_back()) {
389+
if c1 != c2 {
390+
return false;
391+
}
392+
}
393+
true
394+
}
395+
396+
pub fn longest_palindrome(s: String) -> String {
397+
let size = s.len();
398+
let mut ans = &s[..1];
399+
for i in 0..size - 1 {
400+
for j in (i + 1..size).rev() {
401+
if ans.len() > j - i + 1 {
381402
break;
382403
}
383-
if is_pass(i, j) {
384-
res = &s[i..=j];
404+
if Solution::is_palindrome(&s[i..=j]) {
405+
ans = &s[i..=j];
385406
}
386407
}
387408
}
388-
res.into_iter().map(|c| char::from(*c)).collect()
409+
return ans.to_string();
389410
}
390411
}
391412
```

‎solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md

+38-17
Original file line numberDiff line numberDiff line change
@@ -347,30 +347,51 @@ function longestPalindrome(s: string): string {
347347
```rust
348348
impl Solution {
349349
pub fn longest_palindrome(s: String) -> String {
350-
let n = s.len();
351-
let s = s.as_bytes();
352-
let is_pass = |mut l, mut r| {
353-
while l < r {
354-
if s[l] != s[r] {
355-
return false;
350+
let (n, mut ans) = (s.len(), &s[..1]);
351+
let mut dp = vec![vec![false; n]; n];
352+
let data: Vec<char> = s.chars().collect();
353+
354+
for end in 1..n {
355+
for start in 0..=end {
356+
if data[start] == data[end] {
357+
dp[start][end] = end - start < 2 || dp[start + 1][end - 1];
358+
if dp[start][end] && (end - start + 1) > ans.len() {
359+
ans = &s[start..=end];
360+
}
356361
}
357-
l += 1;
358-
r -= 1;
359362
}
360-
true
361-
};
362-
let mut res = &s[0..1];
363-
for i in 0..n - 1 {
364-
for j in (i + 1..n).rev() {
365-
if res.len() > j - i {
363+
}
364+
ans.to_string()
365+
}
366+
}
367+
```
368+
369+
```rust
370+
impl Solution {
371+
pub fn is_palindrome(s: &str) -> bool {
372+
let mut chars = s.chars();
373+
while let (Some(c1), Some(c2)) = (chars.next(), chars.next_back()) {
374+
if c1 != c2 {
375+
return false;
376+
}
377+
}
378+
true
379+
}
380+
381+
pub fn longest_palindrome(s: String) -> String {
382+
let size = s.len();
383+
let mut ans = &s[..1];
384+
for i in 0..size - 1 {
385+
for j in (i + 1..size).rev() {
386+
if ans.len() > j - i + 1 {
366387
break;
367388
}
368-
if is_pass(i, j) {
369-
res = &s[i..=j];
389+
if Solution::is_palindrome(&s[i..=j]) {
390+
ans = &s[i..=j];
370391
}
371392
}
372393
}
373-
res.into_iter().map(|c| char::from(*c)).collect()
394+
return ans.to_string();
374395
}
375396
}
376397
```
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
11
impl Solution {
22
pub fn longest_palindrome(s: String) -> String {
3-
let n = s.len();
4-
let s = s.as_bytes();
5-
let is_pass = |mut l, mut r| {
6-
while l < r {
7-
if s[l] != s[r] {
8-
return false;
9-
}
10-
l += 1;
11-
r -= 1;
12-
}
13-
true
14-
};
15-
let mut res = &s[0..1];
16-
for i in 0..n - 1 {
17-
for j in (i + 1..n).rev() {
18-
if res.len() > j - i {
19-
break;
20-
}
21-
if is_pass(i, j) {
22-
res = &s[i..=j];
3+
let (n, mut ans) = (s.len(), &s[..1]);
4+
let mut dp = vec![vec![false; n]; n];
5+
let data: Vec<char> = s.chars().collect();
6+
7+
for end in 1..n {
8+
for start in 0..=end {
9+
if data[start] == data[end] {
10+
dp[start][end] = end - start < 2 || dp[start + 1][end - 1];
11+
if dp[start][end] && (end - start + 1) > ans.len() {
12+
ans = &s[start..=end];
13+
}
2314
}
2415
}
2516
}
26-
res.into_iter().map(|c| char::from(*c)).collect()
17+
ans.to_string()
2718
}
2819
}
20+

0 commit comments

Comments
 (0)
Please sign in to comment.