Skip to content

Commit 90cf887

Browse files
authored
Merge pull request #59 from tony84727/leetcode/76
feat: leetcode/76
2 parents 2a88264 + 9263c94 commit 90cf887

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

src/leetcode/algorithm_76/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Minimum Window Substring
2+
========================
3+
[leetcode](https://leetcode.com/problems/minimum-window-substring/)

src/leetcode/algorithm_76/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
pub mod sliding_window;
2+
3+
#[cfg(test)]
4+
mod tests {
5+
use super::*;
6+
use test::Bencher;
7+
use test_case::test_case;
8+
9+
#[test_case("ADOBECODEBANC", "ABC", "BANC"; "example 1")]
10+
#[test_case("a", "aa", ""; "case 1")]
11+
#[test_case("a", "a", "a"; "case 2")]
12+
fn test_sliding_window_solution(s: &str, t: &str, expected: &str) {
13+
assert_eq!(
14+
expected.to_string(),
15+
sliding_window::Solution::min_window(s.to_string(), t.to_string())
16+
);
17+
}
18+
19+
#[bench]
20+
fn bench_sliding_window_example_1(b: &mut Bencher) {
21+
b.iter(|| {
22+
sliding_window::Solution::min_window("ADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANCADOBECODEBANC".to_string(), "ABC".to_string());
23+
})
24+
}
25+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use std::collections::HashMap;
2+
3+
pub struct Solution;
4+
5+
struct Scanning {
6+
to_find: HashMap<char, i32>,
7+
missing: u32,
8+
}
9+
10+
impl Scanning {
11+
fn new(target: String) -> Self {
12+
let mut to_find = HashMap::<char, i32>::new();
13+
for c in target.chars() {
14+
*to_find.entry(c).or_default() += 1;
15+
}
16+
let missing = to_find.len() as u32;
17+
Self { to_find, missing }
18+
}
19+
fn meet(&mut self, c: char) {
20+
match self.to_find.get_mut(&c) {
21+
Some(count) => {
22+
if *count == 1 {
23+
self.missing -= 1;
24+
}
25+
*count -= 1;
26+
}
27+
None => (),
28+
}
29+
}
30+
31+
fn remove(&mut self, c: char) {
32+
match self.to_find.get_mut(&c) {
33+
Some(count) => {
34+
if *count == 0 {
35+
self.missing += 1;
36+
}
37+
*count += 1;
38+
}
39+
None => (),
40+
}
41+
}
42+
43+
fn is_satisfied(&self) -> bool {
44+
self.missing == 0
45+
}
46+
47+
fn is_important(&self, c: char) -> bool {
48+
self.to_find.contains_key(&c)
49+
}
50+
}
51+
52+
impl Solution {
53+
pub fn min_window(s: String, t: String) -> String {
54+
let mut scanning = Scanning::new(t);
55+
let mut left = 0;
56+
let mut answer: Option<&[char]> = None;
57+
let s: Vec<char> = s.chars().collect();
58+
for (right, &new) in s.iter().enumerate() {
59+
if scanning.is_important(new) {
60+
scanning.meet(new);
61+
if scanning.is_satisfied() {
62+
for current_left in left..=right {
63+
if scanning.is_important(s[current_left]) {
64+
scanning.remove(s[current_left]);
65+
if !scanning.is_satisfied() {
66+
match answer {
67+
Some(current) if right - current_left < current.len() => {
68+
answer = Some(&s[current_left..=right]);
69+
}
70+
None => {
71+
answer = Some(&s[current_left..=right]);
72+
}
73+
_ => (),
74+
}
75+
left = current_left + 1;
76+
break;
77+
}
78+
}
79+
}
80+
}
81+
}
82+
}
83+
answer.unwrap_or_default().iter().collect()
84+
}
85+
}

src/leetcode/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod algorithm_76;
12
pub mod algorithm_79;
23
pub mod algorithm_73;
34
pub mod add_two_numbers_2;

0 commit comments

Comments
 (0)