Minimum Window Substring - LeetCode 76 - JavaScript

Поділитися
Вставка
  • Опубліковано 19 вер 2024

КОМЕНТАРІ • 12

  • @jritzeku
    @jritzeku 2 місяці тому

    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
    }

  • @ruchighodasara1644
    @ruchighodasara1644 2 роки тому +2

    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.

    • @AlgoJS
      @AlgoJS  2 роки тому

      No problem Ruchi

  • @MukeshSharma-xd4dn
    @MukeshSharma-xd4dn Рік тому

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

  • @pintae8681
    @pintae8681 6 місяців тому

    Great diagramming and explaining the approach.

  • @BTypeGuy
    @BTypeGuy Рік тому

    Well explained and concise. Fantastic Video.

  • @jashwanthg3680
    @jashwanthg3680 Рік тому +1

    Thank you

    • @AlgoJS
      @AlgoJS  Рік тому

      You're welcome!

  • @rydog3000
    @rydog3000 2 роки тому

    Great vid

  • @samarthjain5015
    @samarthjain5015 7 місяців тому

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

  • @amotekundev
    @amotekundev 11 місяців тому

    Thank you