|
4 | 4 | * [Problem link](https://leetcode.com/problems/longest-palindromic-substring/)
|
5 | 5 | */
|
6 | 6 |
|
| 7 | +#![allow(dead_code)] |
| 8 | + |
7 | 9 | struct Solution {}
|
8 | 10 |
|
9 | 11 | // ----------------------------------------------------------------------------
|
10 | 12 |
|
11 | 13 | impl Solution {
|
12 | 14 | pub fn longest_palindrome(s: String) -> String {
|
13 |
| - "bab".to_string() |
| 15 | + if s.len() < 2 { |
| 16 | + return s; |
| 17 | + } |
| 18 | + let chars: Vec<char> = s.chars().collect(); |
| 19 | + let odd_longest: String = { |
| 20 | + let mut longest_part = vec![]; |
| 21 | + for (i, &c) in chars.iter().enumerate() { |
| 22 | + let mut current_part = vec![c]; |
| 23 | + let (mut low, mut high) = (i, i); |
| 24 | + loop { |
| 25 | + if chars[low] != chars[high] { |
| 26 | + // not a palindrome |
| 27 | + break; |
| 28 | + } |
| 29 | + if low != high { |
| 30 | + // exclude the condition only one char |
| 31 | + current_part.push(chars[low]); |
| 32 | + } |
| 33 | + if low > 0 && high < chars.len() - 1 { |
| 34 | + low -= 1; |
| 35 | + high += 1; |
| 36 | + } else { |
| 37 | + break; |
| 38 | + } |
| 39 | + } |
| 40 | + if current_part.len() > longest_part.len() { |
| 41 | + longest_part = current_part; |
| 42 | + } |
| 43 | + } |
| 44 | + let left_part = longest_part[1..] |
| 45 | + .iter() |
| 46 | + .rev() |
| 47 | + .cloned() |
| 48 | + .collect::<Vec<char>>(); |
| 49 | + [left_part, longest_part].concat().iter().cloned().collect() |
| 50 | + }; |
| 51 | + let even_longest: String = { |
| 52 | + let mut longest_part = vec![]; |
| 53 | + for (i, _) in chars[..chars.len() - 1].iter().enumerate() { |
| 54 | + let mut current_part = vec![]; |
| 55 | + let (mut low, mut high) = (i, i + 1); |
| 56 | + loop { |
| 57 | + if chars[low] == chars[high] { |
| 58 | + current_part.push(chars[high]); |
| 59 | + } else { |
| 60 | + break; |
| 61 | + } |
| 62 | + if low > 0 && high < chars.len() - 1 { |
| 63 | + low -= 1; |
| 64 | + high += 1; |
| 65 | + } else { |
| 66 | + break; |
| 67 | + } |
| 68 | + } |
| 69 | + if current_part.len() > longest_part.len() { |
| 70 | + longest_part = current_part; |
| 71 | + } |
| 72 | + } |
| 73 | + let left_part = longest_part.iter().rev().cloned().collect::<Vec<char>>(); |
| 74 | + [left_part, longest_part].concat().iter().cloned().collect() |
| 75 | + }; |
| 76 | + if odd_longest.len() > even_longest.len() { |
| 77 | + odd_longest |
| 78 | + } else { |
| 79 | + even_longest |
| 80 | + } |
14 | 81 | }
|
15 | 82 | }
|
16 | 83 |
|
17 | 84 | #[cfg(test)]
|
18 | 85 | mod tests {
|
19 | 86 | use super::*;
|
20 | 87 |
|
| 88 | + fn accepted(input: String, expected: Vec<String>) { |
| 89 | + assert!(expected.contains(&Solution::longest_palindrome(input))); |
| 90 | + } |
| 91 | + |
21 | 92 | #[test]
|
22 | 93 | fn test_example_1() {
|
23 | 94 | let input = "babad".to_string();
|
24 |
| - let outputs = vec!["bab".to_string(), "aba".to_string()]; |
25 |
| - assert!(outputs.contains(&Solution::longest_palindrome(input))); |
| 95 | + let expected = vec!["bab".to_string(), "aba".to_string()]; |
| 96 | + accepted(input, expected); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test_example_2() { |
| 101 | + let input = "cbbd".to_string(); |
| 102 | + let expected = vec!["bb".to_string()]; |
| 103 | + accepted(input, expected); |
| 104 | + } |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn test_empty_string() { |
| 108 | + let input = "".to_string(); |
| 109 | + let expected = vec!["".to_string()]; |
| 110 | + accepted(input, expected); |
| 111 | + } |
| 112 | + |
| 113 | + #[test] |
| 114 | + fn test_one_string() { |
| 115 | + let input = "a".to_string(); |
| 116 | + let expected = vec!["a".to_string()]; |
| 117 | + accepted(input, expected); |
| 118 | + } |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn test_two_string() { |
| 122 | + let input = "bb".to_string(); |
| 123 | + let expected = vec!["bb".to_string()]; |
| 124 | + accepted(input, expected); |
26 | 125 | }
|
27 | 126 | }
|
0 commit comments