Minimum Window Substring LeetCode solution in Rust

1
impl Solution {
2
pub fn min_window(s: String, t: String) -> String {
3
let mut state = [0 as isize; (b'z' - b'A' + 1) as usize];
4
let mut target = [0 as isize; (b'z' - b'A' + 1) as usize];
5
for b in t.bytes() {
6
target[(b - b'A') as usize] += 1;
7
}
8

9
let target = target;
10
let string = s.as_bytes();
11

12
let mut best_from = 0;
13
let mut best_to = 0;
14

15
let mut from = 0;
16
let mut to = 0;
17

18
let chars_expected = target.iter().filter(|&&ch| ch > 0).count();
19
let mut chars_found = 0;
20

21
while to < string.len() {
22
let ch = (string[to] - b'A') as usize;
23
if target[ch] > 0 {
24
if state[ch] + 1 == target[ch] {
25
chars_found += 1;
26
}
27
state[ch] += 1;
28
}
29

30
if chars_found == chars_expected {
31
let mut fch = (string[from] - b'A') as usize;
32
while target[fch] == 0 || state[fch] > target[fch] {
33
if state[fch] > 0 {
34
state[fch] -= 1;
35
}
36

37
from += 1;
38
fch = (string[from] - b'A') as usize;
39
}
40

41
if to - from < best_to - best_from || best_to == 0 {
42
best_from = from;
43
best_to = to + 1;
44
}
45
}
46

47
to += 1;
48
}
49

50
s[best_from..best_to].to_string()
51
}
52
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0