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 }
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.
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
}
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.
No problem Ruchi
Short & lovely explanation!!! Helped a lot! Thanks
Great diagramming and explaining the approach.
Well explained and concise. Fantastic Video.
Thanks Astro
Thank you
You're welcome!
Great vid
What if we find a windowSlice exactly same as t, if we directly return it, there is no need for further sliding.
Thank you