Minimum Window Substring - LeetCode 76 - JavaScript

preview_player
Показать описание


Step by step walk through of the solution to the popular Meta coding interview question, Minimum Window Substring.

LeetCode 76

JavaScript

0:00 Intro
1:29 Explanation
10:06 Code

#softwareengineering #javascript #leetcode
Рекомендации по теме
Комментарии
Автор

Short and sweet explanation is given. Just woww. Thanks for this video. Before this video I have gone through multiple video I got the logic but still I was pulling out my hair to understand steps to right down the code.

rgrr
Автор

Great explanation! However I ran into some issues so made following changes:

1.)Updated slice (left, right+1) instead

2.) The code here only decrements 'count' when we decrement count when map.get(rLetter) ==0 but in fact it should be >=0 to account for cases
where there are repeated letters such as 'abbc'.


var test = function (s, target) {
let map = new Map()
let minWindow = ''
let minLength = Infinity
let requiredLetters = target.length
let left = 0

// Preprocess our map with target
for (let i = 0; i < target.length; i++) {
map.set(target[i], (map.get(target[i]) || 0) + 1)
}

// Main logic
for (let right = 0; right < s.length; right++) {
//decrement from map and requiredLetters(if needed )
if (map.has(s[right])) {
map.set(s[right], map.get(s[right]) - 1)
//why >=0 and not ==0 ? because our 't' could have duplicate letters like 'ABBC'
if (map.get(s[right]) >= 0) {
requiredLetters--
}
}

// Shrink window until we are short of a required letter
while (requiredLetters == 0) {
//check to update minWindow or not
if (right - left + 1 < minLength) {
minLength = right - left + 1
minWindow = s.slice(left, right + 1)
}

//increment to map and requiredLetters if needed
if (map.has(s[left])) {
map.set(s[left], map.get(s[left]) + 1)
if (map.get(s[left]) > 0) {
requiredLetters++
}
}

left++
}
}

return minWindow
}

jritzeku
Автор

Short & lovely explanation!!! Helped a lot! Thanks

MukeshSharma-xddn
Автор

Great diagramming and explaining the approach.

pintae
Автор

Well explained and concise. Fantastic Video.

BTypeGuy
Автор

What if we find a windowSlice exactly same as t, if we directly return it, there is no need for further sliding.

samarthjain
welcome to shbcf.ru