Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,5 @@
- [Task #15](src/neetcode/sliding-window/best-time-to-buy-and-sell-stock) – _Best Time To Buy And Sell Stock_ - _[check the task](https://neetcode.io/problems/buy-and-sell-crypto)_
- [Task #16](src/neetcode/sliding-window/longest-substring-without-repeating-characters) – _Longest Substring Without Repeating Characters_ - _[check the task](https://neetcode.io/problems/longest-substring-without-duplicates)_
- [Task #17](src/neetcode/sliding-window/longest-repeating-character-replacement) – _Longest Repeating Character Replacement_ - _[check the task](https://neetcode.io/problems/longest-repeating-substring-with-replacement)_
- [Task #18](src/neetcode/sliding-window/permutation-in-string) – _Permutation In String_ - _[check the task](https://neetcode.io/problems/permutation-string)_
- [Task #17](src/neetcode/sliding-window/longest-repeating-character-replacement) – _Longest Repeating Character Replacement_ - _[check the task](https://neetcode.io/problems/longest-repeating-substring-with-replacement)_
- [Task #19](src/neetcode/sliding-window/minimum-window-substring) – _Minimum Window Substring_ - _[check the task](https://neetcode.io/problems/minimum-window-with-characters)_
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { minWindow } from './solution';

describe('Minimum Window Substring | NeetCode | RoadMap | Testcases', () => {
test('#1 Substring containing t characters', () => {
const s = 'OUZODYXAZV';
const t = 'XYZ';
const expected = 'YXAZ';
const output = minWindow(s, t);
expect(output).toBe(expected);
});

test('#2 Exact t string', () => {
const s = 'xyz';
const t = 'xyz';
const expected = 'xyz';
const output = minWindow(s, t);
expect(output).toBe(expected);
});

test('#3 No such substring', () => {
const s = 'x';
const t = 'xy';
const expected = '';
const output = minWindow(s, t);
expect(output).toBe(expected);
});
});
49 changes: 49 additions & 0 deletions src/neetcode/sliding-window/minimum-window-substring/solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @param {string} s - first string
* @param {string} t - second string
* @return {string} the shortest substring of `s` such that every character in `t`, including duplicates,
* is present in the substring. If such a substring does not exist, an empty string `""` will be returned.
*/
export const minWindow = (s: string, t: string): string => {
if (t.length > s.length) {
return '';
}

let mxLen = Infinity;
let fulfilled = 0;

const tChars: Record<string, number | undefined> = {};
const sChars: Record<string, number | undefined> = {};
const resIdx = [-1, -1];

for (const ch of t) {
tChars[ch] = (tChars[ch] ?? 0) + 1;
}

const need = Object.keys(tChars).length;
let l = 0;

for (let r = 0; r < s.length; r++) {
sChars[s[r]] = (sChars[s[r]] ?? 0) + 1;

if (sChars[s[r]] === tChars[s[r]]) {
fulfilled++;
}

while (fulfilled === need) {
if (r - l + 1 < mxLen) {
mxLen = r - l + 1;
resIdx[0] = l;
resIdx[1] = r + 1;
}

sChars[s[l]] = (sChars[s[l]] ?? 0) - 1;
if ((sChars[s[l]] ?? 0) < (tChars[s[l]] ?? 0)) {
fulfilled--;
}
l++;
}
}

return mxLen === Infinity ? '' : s.slice(resIdx[0], resIdx[1]);
};
Loading