-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
35 lines (31 loc) · 863 Bytes
/
mod.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
pub struct Solution;
impl Solution {
pub fn minimum_deletions(s: String) -> i32 {
let mut left_b = vec![0; s.len() + 1];
let mut right_a = vec![0; s.len() + 1];
for (i, b) in s.bytes().enumerate() {
left_b[i + 1] = left_b[i] + if b == b'b' { 1 } else { 0 };
}
for (i, b) in s.bytes().enumerate().rev() {
right_a[i] = right_a[i + 1] + if b == b'a' { 1 } else { 0 };
}
left_b
.into_iter()
.zip(right_a)
.map(|(b, a)| b + a)
.min()
.unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test1() {
assert_eq!(Solution::minimum_deletions("aababbab".to_string()), 2);
}
#[test]
fn test2() {
assert_eq!(Solution::minimum_deletions("bbaaaaabb".to_string()), 2);
}
}