Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ce159ff

Browse files
committedAug 14, 2023
feat: finish problem 76 related with minimum window substring
1 parent 58754e7 commit ce159ff

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed
 

‎src/hard/minimum_window_substring.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,43 @@
11
#![allow(dead_code)]
22
pub fn min_window(s: String, t: String) -> String {
3-
"".to_string()
3+
if t.len() > s.len() {
4+
return "".to_string();
5+
}
6+
7+
let need_map = t.chars().fold([0; 128], |mut acc, c| {
8+
acc[c as usize] += 1;
9+
acc
10+
});
11+
let mut window_map = [0; 128];
12+
let mut left = 0;
13+
let mut right = 0;
14+
let mut count = 0;
15+
let mut min_substring = "".to_string();
16+
17+
while right < s.len() {
18+
let c = s.chars().nth(right).unwrap();
19+
window_map[c as usize] += 1;
20+
if window_map[c as usize] <= need_map[c as usize] {
21+
count += 1;
22+
}
23+
24+
// If the window contains all the characters in t
25+
// Try to shrink the window
26+
while count == t.len() {
27+
let c = s.chars().nth(left).unwrap();
28+
if right - left + 1 < min_substring.len() || min_substring.is_empty() {
29+
min_substring = s[left..=right].to_string();
30+
}
31+
window_map[c as usize] -= 1;
32+
if window_map[c as usize] < need_map[c as usize] {
33+
count -= 1;
34+
}
35+
left += 1;
36+
}
37+
right += 1;
38+
}
39+
40+
min_substring
441
}
542

643
#[cfg(test)]
@@ -13,4 +50,18 @@ mod tests {
1350
let t = "ABC".to_string();
1451
assert_eq!(min_window(s, t), "BANC".to_string());
1552
}
53+
54+
#[test]
55+
fn test_min_window_1() {
56+
let s = "a".to_string();
57+
let t = "aa".to_string();
58+
assert_eq!(min_window(s, t), "".to_string());
59+
}
60+
61+
#[test]
62+
fn test_min_window_2() {
63+
let s = "a".to_string();
64+
let t = "a".to_string();
65+
assert_eq!(min_window(s, t), "a".to_string());
66+
}
1667
}

‎src/readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,6 @@
5656
- [x] [739. Daily temperatures](../src/medium/daily_temperatures.rs)
5757
- [x] [853. Car fleet](../src/medium/car_fleet.rs)
5858
- [Hard](../src/hard)
59-
- [x] [84. Largest rectangle in histogram](../src/hard/largest_rectangle_in_histogram.rs)
6059
- [x] [42. Trapping rain water](../src/hard/trapping_rain_water.rs)
60+
- [x] [76. Minimum window substring](../src/hard/minimum_window_substring.rs)
61+
- [x] [84. Largest rectangle in histogram](../src/hard/largest_rectangle_in_histogram.rs)

‎theory/categories/3.sliding_windows/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
- [x] [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | Medium | [Solution](../../../src/medium/longest_substring_without_repeating_characters.rs)
2929
- [x] [Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/) | Medium | [Solution](../../../src/medium/longest_repeating_character_replacement.rs)
3030
- [x] [Permutation in String](https://leetcode.com/problems/permutation-in-string/) | Medium | [Solution](../../../src/medium/permutation_in_string.rs)
31-
- [ ] [Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/) | Hard | [Solution](../../../src/hard/minimum_window_substring.rs)
31+
- [x] [Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/) | Hard | [Solution](../../../src/hard/minimum_window_substring.rs)
3232
- [ ] [Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/) | Hard | [Solution](../../../src/hard/sliding_window_maximum.rs)
3333

3434
Category: `Sliding Windows`
3535
Created on: 2023-07-28 15:14:00
36-
Last modified on: 2023-08-10 21:05:00
36+
Last modified on: 2023-08-14 16:07:00
3737
Status: In Progress
3838
Author: [David Bujosa](https://github.com/bujosa)

0 commit comments

Comments
 (0)
Please sign in to comment.