-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimum_window_substring.rs
82 lines (73 loc) · 2.28 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
///
/// Problem: Minimum Window Substring
///
/// Given two strings `s` and `t`, return the **smallest substring in `s`** that contains **all characters of `t`** in any order.
///
/// If such a substring does not exist, return an **empty string**.
///
/// **Example 1:**
/// ```plaintext
/// Input: s = "ADOBECODEBANC", t = "ABC"
/// Output: "BANC"
/// ```
///
/// **Example 2:**
/// ```plaintext
/// Input: s = "a", t = "a"
/// Output: "a"
/// ```
///
/// **Example 3:**
/// ```plaintext
/// Input: s = "a", t = "aa"
/// Output: ""
/// Explanation: `t` requires two 'a's, but `s` only has one.
/// ```
///
/// **Constraints:**
/// - `1 <= s.length, t.length <= 10^5`
/// - `s` and `t` consist of **uppercase and lowercase English letters**.
///
/// # Solution:
/// - **Time Complexity:** `O(n)`
/// - **Space Complexity:** `O(1)`
use std::collections::HashMap;
impl Solution {
pub fn min_window(s: String, t: String) -> String {
let (s_bytes, t_bytes) = (s.as_bytes(), t.as_bytes());
let mut target_count = HashMap::new();
let mut window_count = HashMap::new();
for &c in t_bytes {
*target_count.entry(c).or_insert(0) += 1;
}
let (mut left, mut right) = (0, 0);
let (mut min_len, mut min_start) = (usize::MAX, 0);
let mut required = target_count.len();
let mut formed = 0;
while right < s_bytes.len() {
let c = s_bytes[right];
*window_count.entry(c).or_insert(0) += 1;
if target_count.contains_key(&c) && window_count[&c] == target_count[&c] {
formed += 1;
}
while formed == required {
let start_char = s_bytes[left];
if right - left + 1 < min_len {
min_len = right - left + 1;
min_start = left;
}
*window_count.get_mut(&start_char).unwrap() -= 1;
if target_count.contains_key(&start_char) && window_count[&start_char] < target_count[&start_char] {
formed -= 1;
}
left += 1;
}
right += 1;
}
if min_len == usize::MAX {
"".to_string()
} else {
s[min_start..min_start + min_len].to_string()
}
}
}