-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimum_window_substring.rs
145 lines (125 loc) · 4.3 KB
/
minimum_window_substring.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use std::collections::HashMap;
/// Given two strings `s` and `t` of lengths `m` and `n` respectively, return the minimum window
/// substring of `s` such that every character in `t` (including duplicates) is included in the
/// window. If there is no such substring, return the empty string `""`.
///
/// The testcases will be generated such that the answer is unique.
///
struct Solution;
impl Solution {
fn counts(t: String) -> HashMap::<char, usize> {
let mut result = HashMap::new();
for letter in t.chars() {
result
.entry(letter)
.and_modify(|count| *count += 1)
.or_insert(1);
}
result
}
fn contains(s_counts: &HashMap<char, usize>, t_counts: &HashMap<char, usize>) -> bool {
let mut result = true;
for (key, value) in t_counts {
result = s_counts.get(key).copied().unwrap_or(0) >= *value;
if !result {
break;
}
}
result
}
pub fn min_window(s: String, t: String) -> String {
let mut result = "".to_string();
let s_len = s.len();
let t_len = t.len();
if t_len > s_len || s_len == 0 || t_len == 0 {
result
} else {
let s_letters: Vec<char> = s.chars().collect();
let t_counts: HashMap<char, usize> = Self::counts(t);
let mut s_counts: HashMap<char, usize> = HashMap::new();
for i in 0..t_len {
let letter = s_letters[i];
s_counts
.entry(letter)
.and_modify(|count| *count += 1)
.or_insert(1);
}
let mut left = 0;
let mut right = t_len - 1;
loop {
if Self::contains(&s_counts, &t_counts) {
let length = right - left + 1;
if result.len() == 0 || result.len() > length {
result = s_letters[left..=right].iter().collect();
}
loop {
if left == s_len - 1 {
break;
} else {
let letter = s_letters[left];
s_counts
.entry(letter)
.and_modify(|count| *count -= 1);
left += 1;
if Self::contains(&s_counts, &t_counts) {
let length = right - left + 1;
if result.len() > length {
result = s_letters[left..=right].iter().collect();
}
} else {
s_counts
.entry(letter)
.and_modify(|count| *count += 1);
left -= 1;
break;
}
}
}
}
if right == s_len - 1 {
break;
} else {
right += 1;
let letter = s_letters[right];
s_counts
.entry(letter)
.and_modify(|count| *count += 1)
.or_insert(1);
}
}
result
}
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn example_1() {
let s = "ADOBECODEBANC".to_string();
let t = "ABC".to_string();
let result = Solution::min_window(s, t);
assert_eq!(result, "BANC");
}
#[test]
fn example_2() {
let s = "a".to_string();
let t = "a".to_string();
let result = Solution::min_window(s, t);
assert_eq!(result, "a");
}
#[test]
fn example_3() {
let s = "a".to_string();
let t = "aa".to_string();
let result = Solution::min_window(s, t);
assert_eq!(result, "");
}
#[test]
fn example_4() {
let s = "cabwefgewcwaefgcf".to_string();
let t = "cae".to_string();
let result = Solution::min_window(s, t);
assert_eq!(result, "cwae");
}
}