LeetCode 727. Minimum Window Subsequence

Problem Statement


impl Solution {
pub fn min_window(s: String, t: String) -> String {
let (m, n, mut start, mut min_len) = (s.len(), t.len(), -1, std::i32::MAX);
let mut dp : Vec<Vec<i32>> = vec![vec![-1; n + 1]; m + 1];
for i in 0..m + 1 {
dp[i][0] = i as i32;
}
for i in 1..m + 1 {
for j in 1..std::cmp::min(i, n) + 1 {
if s.chars().nth(i - 1).unwrap() == t.chars().nth(j - 1).unwrap() {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = dp[i - 1][j];
}
}

if dp[i][n] != -1 {
let len = i - dp[i][n] as usize;
if min_len as usize > len {
min_len = len as i32;
start = dp[i][n];
}
}
}

if start != -1 {
let slice = &s[start as usize..(start + min_len) as usize];
return (slice.to_string().clone()).parse().unwrap();
} else {
return "".to_string();
}
}
}