From 10ace265d3d6b0c4c010ada94a58b85673ca33ac Mon Sep 17 00:00:00 2001 From: ivan Date: Sat, 4 Jul 2026 05:17:56 -0600 Subject: [PATCH] adding algo --- .../round_1/10_minimum_window_substring.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/my_project/interviews/google_top_exercises/round_1/10_minimum_window_substring.py diff --git a/src/my_project/interviews/google_top_exercises/round_1/10_minimum_window_substring.py b/src/my_project/interviews/google_top_exercises/round_1/10_minimum_window_substring.py new file mode 100644 index 00000000..403fdf7b --- /dev/null +++ b/src/my_project/interviews/google_top_exercises/round_1/10_minimum_window_substring.py @@ -0,0 +1,30 @@ +from collections import Counter + +class Solution: + def minWindow(self, s: str, t: str) -> str: + if not s or not t or len(s) < len(t): + return "" + + need = Counter(t) # chars (with counts) still required + missing = len(t) # total chars still to satisfy + left = 0 + best_left, best_len = 0, float("inf") + + for right, char in enumerate(s): + # This char helps only if we still need it (count > 0) + if need[char] > 0: + missing -= 1 + need[char] -= 1 + + # Window covers all of t: try to shrink from the left + while missing == 0: + if right - left + 1 < best_len: + best_left, best_len = left, right - left + 1 + + need[s[left]] += 1 + # Dropping this char breaks coverage only if it was required + if need[s[left]] > 0: + missing += 1 + left += 1 + + return "" if best_len == float("inf") else s[best_left:best_left + best_len] \ No newline at end of file