|
| 1 | +// 02_minimum_window_substring.cpp |
| 2 | +// |
| 3 | +// 🔍 Algorithm: Minimum Window Substring |
| 4 | +// Description: |
| 5 | +// Finds the smallest substring in `source` that contains all |
| 6 | +// characters of `target` (including duplicates). |
| 7 | +// Uses a Sliding Window approach combined with frequency maps |
| 8 | +// to expand and contract the window optimally. |
| 9 | +// |
| 10 | +// Use Case: |
| 11 | +// Useful in text search problems, DNA sequence matching, |
| 12 | +// or when finding minimal segments containing specific data. |
| 13 | +// |
| 14 | +// Time Complexity: O(|S| + |T|) |
| 15 | +// Space Complexity: O(|T|) |
| 16 | +// |
| 17 | +// LeetCode: https://leetcode.com/problems/minimum-window-substring/ |
| 18 | + |
| 19 | + |
| 20 | +#include <iostream> |
| 21 | +#include <string> |
| 22 | +#include <unordered_map> |
| 23 | +#include <climits> |
| 24 | +using namespace std; |
| 25 | + |
| 26 | +string minWindow(const string &source, const string &target) { |
| 27 | + if (target.empty() || source.empty() || source.size() < target.size()) return ""; |
| 28 | + |
| 29 | + unordered_map<char,int> need; |
| 30 | + for (char c : target) need[c]++; |
| 31 | + |
| 32 | + int required = need.size(); // distinct characters needed |
| 33 | + int formed = 0; // how many distinct characters currently satisfy required count |
| 34 | + unordered_map<char,int> windowCounts; |
| 35 | + |
| 36 | + int left = 0, right = 0; |
| 37 | + int bestLeft = 0; |
| 38 | + int bestLen = INT_MAX; |
| 39 | + |
| 40 | + while (right < (int)source.size()) { |
| 41 | + char c = source[right]; |
| 42 | + windowCounts[c]++; |
| 43 | + |
| 44 | + // If current char count matches the required count, we've satisfied one distinct char |
| 45 | + if (need.count(c) && windowCounts[c] == need[c]) { |
| 46 | + formed++; |
| 47 | + } |
| 48 | + |
| 49 | + // When all required chars are satisfied, try to shrink from the left |
| 50 | + while (left <= right && formed == required) { |
| 51 | + if (right - left + 1 < bestLen) { |
| 52 | + bestLen = right - left + 1; |
| 53 | + bestLeft = left; |
| 54 | + } |
| 55 | + |
| 56 | + char leftChar = source[left]; |
| 57 | + windowCounts[leftChar]--; |
| 58 | + if (need.count(leftChar) && windowCounts[leftChar] < need[leftChar]) { |
| 59 | + formed--; |
| 60 | + } |
| 61 | + left++; |
| 62 | + } |
| 63 | + |
| 64 | + right++; |
| 65 | + } |
| 66 | + |
| 67 | + if (bestLen == INT_MAX) return ""; |
| 68 | + return source.substr(bestLeft, bestLen); |
| 69 | +} |
| 70 | + |
| 71 | +int main() { |
| 72 | + cout << "Minimum Window Substring\n"; |
| 73 | + cout << "Input format (two lines):\nS\nT\n\nEnter S (source string) and T (target string) each on a new line:\n"; |
| 74 | + |
| 75 | + string S, T; |
| 76 | + if (!getline(cin, S)) return 0; |
| 77 | + if (!getline(cin, T)) return 0; |
| 78 | + |
| 79 | + string answer = minWindow(S, T); |
| 80 | + if (answer.empty()) { |
| 81 | + cout << "No valid window found\n"; |
| 82 | + } else { |
| 83 | + cout << "Minimum window: " << answer << "\n"; |
| 84 | + } |
| 85 | + return 0; |
| 86 | +} |
| 87 | + |
| 88 | +/* |
| 89 | +Example: |
| 90 | +S = "ADOBECODEBANC" |
| 91 | +T = "ABC" |
| 92 | +Output: "BANC" |
| 93 | +*/ |
0 commit comments