Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added minimum window substring implementation and test file.#1821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Mayank29903 wants to merge2 commits intoTheAlgorithms:master
base:master
Choose a base branch
Loading
fromMayank29903:add-min-window-sub
Open
Show file tree
Hide file tree
Changes fromall 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
4 changes: 2 additions & 2 deletionsMaths/MobiusFunction.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,6 @@ export const mobiusFunction = (number) => {
return primeFactorsArray.length !== new Set(primeFactorsArray).size
? 0
: primeFactorsArray.length % 2 === 0
? 1
: -1
? 1
: -1
}
42 changes: 42 additions & 0 deletionsSliding-Windows/MinimumWindowSubstring.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -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 }
17 changes: 17 additions & 0 deletionsSliding-Windows/test/MinimumWindowSubstring.test.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -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')
})
Loading

[8]ページ先頭

©2009-2025 Movatter.jp