|
| 1 | +// 02_minimum_window_substring.cpp |
| 2 | +// Problem: Minimum Window Substring |
| 3 | +// LeetCode: https://leetcode.com/problems/minimum-window-substring/ |
| 4 | +// Author: DeveloperViraj (curated DSA set for Hacktoberfest 2025) |
| 5 | +// Compile: g++ -std=c++17 02_minimum_window_substring.cpp -o 02_minimum_window_substring |
| 6 | +// Run: ./02_minimum_window_substring |
| 7 | +// |
| 8 | +// Summary: |
| 9 | +// Given strings S and T, find the smallest substring in S that contains all characters of T |
| 10 | +// (including multiplicity). If no such window exists, return an empty string. |
| 11 | +// |
| 12 | +// Approach (sliding window + frequency maps): |
| 13 | +// 1. Build a frequency map for characters required by T. |
| 14 | +// 2. Expand 'right' pointer: add chars to current window counts. |
| 15 | +// 3. When the window satisfies all required counts (formed == required), try to shrink 'left' |
| 16 | +// to minimize the window while still satisfying the requirement. |
| 17 | +// 4. Track best (smallest) window found during the process. |
| 18 | +// |
| 19 | +// Time Complexity: O(|S| + |T|) average (each char processed a constant number of times). |
| 20 | +// Space Complexity: O(CHARSET) or O(|T|) for maps. |
| 21 | + |
| 22 | +#include <iostream> |
| 23 | +#include <string> |
| 24 | +#include <unordered_map> |
| 25 | +#include <climits> |
| 26 | +using namespace std; |
| 27 | + |
| 28 | +string minWindow(const string &source, const string &target) { |
| 29 | + if (target.empty() || source.empty() || source.size() < target.size()) return ""; |
| 30 | + |
| 31 | + unordered_map<char,int> need; |
| 32 | + for (char c : target) need[c]++; |
| 33 | + |
| 34 | + int required = need.size(); // distinct characters needed |
| 35 | + int formed = 0; // how many distinct characters currently satisfy required count |
| 36 | + unordered_map<char,int> windowCounts; |
| 37 | + |
| 38 | + int left = 0, right = 0; |
| 39 | + int bestLeft = 0; |
| 40 | + int bestLen = INT_MAX; |
| 41 | + |
| 42 | + while (right < (int)source.size()) { |
| 43 | + char c = source[right]; |
| 44 | + windowCounts[c]++; |
| 45 | + |
| 46 | + // If current char count matches the required count, we've satisfied one distinct char |
| 47 | + if (need.count(c) && windowCounts[c] == need[c]) { |
| 48 | + formed++; |
| 49 | + } |
| 50 | + |
| 51 | + // When all required chars are satisfied, try to shrink from the left |
| 52 | + while (left <= right && formed == required) { |
| 53 | + if (right - left + 1 < bestLen) { |
| 54 | + bestLen = right - left + 1; |
| 55 | + bestLeft = left; |
| 56 | + } |
| 57 | + |
| 58 | + char leftChar = source[left]; |
| 59 | + windowCounts[leftChar]--; |
| 60 | + if (need.count(leftChar) && windowCounts[leftChar] < need[leftChar]) { |
| 61 | + formed--; |
| 62 | + } |
| 63 | + left++; |
| 64 | + } |
| 65 | + |
| 66 | + right++; |
| 67 | + } |
| 68 | + |
| 69 | + if (bestLen == INT_MAX) return ""; |
| 70 | + return source.substr(bestLeft, bestLen); |
| 71 | +} |
| 72 | + |
| 73 | +int main() { |
| 74 | + cout << "Minimum Window Substring\n"; |
| 75 | + cout << "Input format (two lines):\nS\nT\n\nEnter S (source string) and T (target string) each on a new line:\n"; |
| 76 | + |
| 77 | + string S, T; |
| 78 | + if (!getline(cin, S)) return 0; |
| 79 | + if (!getline(cin, T)) return 0; |
| 80 | + |
| 81 | + string answer = minWindow(S, T); |
| 82 | + if (answer.empty()) { |
| 83 | + cout << "No valid window found\n"; |
| 84 | + } else { |
| 85 | + cout << "Minimum window: " << answer << "\n"; |
| 86 | + } |
| 87 | + return 0; |
| 88 | +} |
| 89 | + |
| 90 | +/* |
| 91 | +Example: |
| 92 | +S = "ADOBECODEBANC" |
| 93 | +T = "ABC" |
| 94 | +Output: "BANC" |
| 95 | +*/ |
0 commit comments