1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
struct Solution; use std::i32; impl Solution { fn longest_palindrome_subseq(s: String) -> i32 { let s: Vec<char> = s.chars().collect(); let n = s.len(); let mut dp: Vec<Vec<i32>> = vec![vec![0; n + 1]; n]; for i in 0..n { dp[i][i + 1] = 1; } for w in 2..=n { for i in 0..=n - w { let j = i + w; if s[i] == s[j - 1] { dp[i][j] = 2 + dp[i + 1][j - 1]; } else { dp[i][j] = i32::max(dp[i + 1][j], dp[i][j - 1]); } } } dp[0][n] } } #[test] fn test() { let s = "bbbab".to_string(); let res = 4; assert_eq!(Solution::longest_palindrome_subseq(s), res); let s = "cbbd".to_string(); let res = 2; assert_eq!(Solution::longest_palindrome_subseq(s), res); }