aboutsummaryrefslogtreecommitdiff
path: root/other/min_window_substring.rs
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/min_window_substring.rs
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/min_window_substring.rs')
-rw-r--r--other/min_window_substring.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/other/min_window_substring.rs b/other/min_window_substring.rs
new file mode 100644
index 0000000..f23402b
--- /dev/null
+++ b/other/min_window_substring.rs
@@ -0,0 +1,43 @@
+struct Solution;
+
+impl Solution {
+ pub fn min_window(s: String, t: String) -> String {
+ let mut state = [0 as isize; (b'z' - b'A' + 1) as usize];
+ let mut target = [0 as isize; (b'z' - b'A' + 1) as usize];
+ for b in t.bytes() {
+ target[(b - b'A') as usize] += 1;
+ }
+ let (target, string, mut best_from, mut best_to, mut from, mut to, mut chars_found, chars_expected) =
+ (target, s.as_bytes(), 0, 0, 0, 0, 0, target.iter().filter(|&&ch| ch > 0).count());
+ while to < string.len() {
+ let ch = (string[to] - b'A') as usize;
+ if target[ch] > 0 {
+ if state[ch] + 1 == target[ch] {
+ chars_found += 1;
+ }
+ state[ch] += 1;
+ }
+ if chars_found == chars_expected {
+ let mut fch = (string[from] - b'A') as usize;
+ while target[fch] == 0 || state[fch] > target[fch] {
+ if state[fch] > 0 {
+ state[fch] -= 1;
+ }
+ from += 1;
+ fch = (string[from] - b'A') as usize;
+ }
+ if to - from < best_to - best_from || best_to == 0 {
+ best_from = from;
+ best_to = to + 1;
+ }
+ }
+ to += 1;
+ }
+ s[best_from..best_to].to_string()
+ }
+}
+
+fn main() {
+ let (s, t) = (String::from("ADOBECODEBANC"), String::from("ABC"));
+ print!("{}", Solution::min_window(s, t));
+}