-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest_palindromic_sequence.rs
90 lines (69 loc) · 1.98 KB
/
longest_palindromic_sequence.rs
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// https://leetcode.com/problems/longest-palindromic-subsequence/
use std::cmp::max;
use std::collections::HashMap;
// TODO: Implement a DP approach which would be faster
pub fn longest_palindrome_subseq(s: String) -> i32 {
fn solve<'a>(s: &'a str, cache: &mut HashMap<&'a str, i32> ) -> i32 {
if let Some(&result) = cache.get(s) {
return result;
}
let length = s.len();
if length < 2 {
return length as i32;
}
let first_letter = &s[..1];
let last_letter = &s[length-1..];
let solution =
if first_letter == last_letter {
2 + solve(&s[1..length-1], cache)
} else {
max(
solve(&s[1..length], cache),
solve(&s[..length-1], cache),
)
};
cache.insert(s, solution);
solution
}
let mut cache: HashMap<&str, i32> = HashMap::new();
solve(&s, &mut cache)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_empty() {
let actual = longest_palindrome_subseq("".to_string());
assert_eq!(actual, 0);
}
#[test]
fn test_single() {
let actual = longest_palindrome_subseq("a".to_string());
assert_eq!(actual, 1);
}
#[test]
fn test_double_not_same() {
let actual = longest_palindrome_subseq("ab".to_string());
assert_eq!(actual, 1);
}
#[test]
fn test_double_same() {
let actual = longest_palindrome_subseq("aa".to_string());
assert_eq!(actual, 2);
}
#[test]
fn test_triple() {
let actual = longest_palindrome_subseq("aba".to_string());
assert_eq!(actual, 3);
}
#[test]
fn test_1() {
let actual = longest_palindrome_subseq("bbbab".to_string());
assert_eq!(actual, 4);
}
#[test]
fn test_2() {
let actual = longest_palindrome_subseq("cbbd".to_string());
assert_eq!(actual, 2);
}
}