From 8661dd37db614e7ae76cb252d8795f04136eb90d Mon Sep 17 00:00:00 2001 From: Mayank Date: Sat, 4 Oct 2025 12:42:49 +0530 Subject: [PATCH 1/2] Added minimum window substring implementation and test file --- Sliding-Windows/MinimumWindowSubstring.js | 42 +++++++++++++++++++ .../test/MinimumWindowSubstring.test.js | 17 ++++++++ 2 files changed, 59 insertions(+) create mode 100644 Sliding-Windows/MinimumWindowSubstring.js create mode 100644 Sliding-Windows/test/MinimumWindowSubstring.test.js diff --git a/Sliding-Windows/MinimumWindowSubstring.js b/Sliding-Windows/MinimumWindowSubstring.js new file mode 100644 index 0000000000..8977c093c6 --- /dev/null +++ b/Sliding-Windows/MinimumWindowSubstring.js @@ -0,0 +1,42 @@ +/* + * Author: Mayank + * Minimum Window Substring implementation in JavaScript + * Finds the smallest substring of s that contains all characters of t. + */ + +function minWindowSubstring(s, t) { + if (t.length > s.length) return "" + + const need = {} + for (let char of t) { + need[char] = (need[char] || 0) + 1 + } + + let left = 0 + let count = t.length + let minLen = Infinity + let minStart = 0 + + for (let right = 0; right < s.length; right++) { + if (need[s[right]] !== undefined) { + if (need[s[right]] > 0) count-- + need[s[right]]-- + } + + while (count === 0) { + if (right - left + 1 < minLen) { + minLen = right - left + 1 + minStart = left + } + if (need[s[left]] !== undefined) { + need[s[left]]++ + if (need[s[left]] > 0) count++ + } + left++ + } + } + + return minLen === Infinity ? "" : s.substring(minStart, minStart + minLen) +} + +export { minWindowSubstring } diff --git a/Sliding-Windows/test/MinimumWindowSubstring.test.js b/Sliding-Windows/test/MinimumWindowSubstring.test.js new file mode 100644 index 0000000000..7fd48683a8 --- /dev/null +++ b/Sliding-Windows/test/MinimumWindowSubstring.test.js @@ -0,0 +1,17 @@ +/* + * Test file for Minimum Window Substring + */ + +import { minWindowSubstring } from "../MinimumWindowSubstring.js" + +test("return smallest window containing all characters", () => { + expect(minWindowSubstring("ADOBECODEBANC", "ABC")).toBe("BANC") +}) + +test("return empty string if no window found", () => { + expect(minWindowSubstring("HELLO", "XYZ")).toBe("") +}) + +test("return full string if it matches exactly", () => { + expect(minWindowSubstring("ABC", "ABC")).toBe("ABC") +}) From 64137f5e725484941fb9669cd8db790a2ecd00a3 Mon Sep 17 00:00:00 2001 From: Mayank Date: Sat, 4 Oct 2025 13:14:02 +0530 Subject: [PATCH 2/2] Added minimum window substring implementation and test file with prettier code --- Maths/MobiusFunction.js | 4 ++-- Sliding-Windows/MinimumWindowSubstring.js | 4 ++-- .../test/MinimumWindowSubstring.test.js | 14 +++++++------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Maths/MobiusFunction.js b/Maths/MobiusFunction.js index bd268b8bbd..4239d6ab31 100644 --- a/Maths/MobiusFunction.js +++ b/Maths/MobiusFunction.js @@ -28,6 +28,6 @@ export const mobiusFunction = (number) => { return primeFactorsArray.length !== new Set(primeFactorsArray).size ? 0 : primeFactorsArray.length % 2 === 0 - ? 1 - : -1 + ? 1 + : -1 } diff --git a/Sliding-Windows/MinimumWindowSubstring.js b/Sliding-Windows/MinimumWindowSubstring.js index 8977c093c6..1616c3a6b7 100644 --- a/Sliding-Windows/MinimumWindowSubstring.js +++ b/Sliding-Windows/MinimumWindowSubstring.js @@ -5,7 +5,7 @@ */ function minWindowSubstring(s, t) { - if (t.length > s.length) return "" + if (t.length > s.length) return '' const need = {} for (let char of t) { @@ -36,7 +36,7 @@ function minWindowSubstring(s, t) { } } - return minLen === Infinity ? "" : s.substring(minStart, minStart + minLen) + return minLen === Infinity ? '' : s.substring(minStart, minStart + minLen) } export { minWindowSubstring } diff --git a/Sliding-Windows/test/MinimumWindowSubstring.test.js b/Sliding-Windows/test/MinimumWindowSubstring.test.js index 7fd48683a8..c32371ef6e 100644 --- a/Sliding-Windows/test/MinimumWindowSubstring.test.js +++ b/Sliding-Windows/test/MinimumWindowSubstring.test.js @@ -2,16 +2,16 @@ * Test file for Minimum Window Substring */ -import { minWindowSubstring } from "../MinimumWindowSubstring.js" +import { minWindowSubstring } from '../MinimumWindowSubstring.js' -test("return smallest window containing all characters", () => { - expect(minWindowSubstring("ADOBECODEBANC", "ABC")).toBe("BANC") +test('return smallest window containing all characters', () => { + expect(minWindowSubstring('ADOBECODEBANC', 'ABC')).toBe('BANC') }) -test("return empty string if no window found", () => { - expect(minWindowSubstring("HELLO", "XYZ")).toBe("") +test('return empty string if no window found', () => { + expect(minWindowSubstring('HELLO', 'XYZ')).toBe('') }) -test("return full string if it matches exactly", () => { - expect(minWindowSubstring("ABC", "ABC")).toBe("ABC") +test('return full string if it matches exactly', () => { + expect(minWindowSubstring('ABC', 'ABC')).toBe('ABC') })