Skip to content

Commit

Permalink
Feat: 76. Minimum Window Substring
Browse files Browse the repository at this point in the history
  • Loading branch information
HC-kang committed Oct 9, 2024
1 parent aff0d63 commit cd17daa
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions minimum-window-substring/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* https://leetcode.com/problems/minimum-window-substring
* T.C. O(s + t)
* S.C. O(t)
*/
function minWindow(s: string, t: string): string {
let minLow = 0;
let minHigh = s.length;

const counts: Record<string, number> = {};
for (const c of t) {
counts[c] = (counts[c] || 0) + 1;
}

let included = 0;

let low = 0;
for (let high = 0; high < s.length; high++) {
if (counts[s[high]]) {
if (counts[s[high]] > 0) {
included++;
}
counts[s[high]]--;
}

while (included === t.length) {
if (high - low < minHigh - minLow) {
minLow = low;
minHigh = high;
}

if (counts[s[low]]) {
counts[s[low]]++;
if (counts[s[low]] > 0) {
included--;
}
}

low++;
}
}

return minHigh === s.length ? '' : s.substring(minLow, minHigh + 1);
}

0 comments on commit cd17daa

Please sign in to comment.