SOURCE

/**
 * @param source : A string
 * @param target: A string
 * @return: A string denote the minimum window, return "" if there is no such a string
 */
const minWindow = function (source , target) {
    const tar = {};
    for(let i=0;i<target.length;i++) {      //put target character in a map
        const key = target[i];
        if(tar[key]) {
            tar[key]++;
        } else {
            tar[key]=1;
        }
    }
    let pointer1 = pointer2 = 0;
    let count = 0;
    const results= [];
    
    while(pointer2 < source.length) {
        const current = source[pointer2];
        if(typeof tar[current] !== 'undefined') {  // if current value is in map
            tar[current]--;
            if(tar[current] >= 0) {
                console.log(current);
              count++;
               if(count === target.length) {
                  results.push(source.slice(pointer1, pointer2+1));
                  while(pointer1 < pointer2) {
                      let current1 = source[pointer1];
                      pointer1++;
                      if(typeof tar[current1] !== 'undefined') {
                          tar[current1] ++;
                          if(tar[current1] === 0) {
                              results.push(source.slice(pointer1, pointer2+1));
                          } else {
                              count--;
                              pointer2++;
                              break;
                          }
                      } else {
                          pointer1++;
                      }
                      
                  }
              } else {
                  pointer2++;
              }
            } else {
                pointer2++;
            }
        } else {
            pointer2 ++;
        }
    }
    console.log(results);

    let minResult = '';
    results.forEach(v => {
        if(v.length < minResult.length) {
            minResult = v;
        }
    });
    return minResult;
}

const source = "ADOBECODEBANC";
const target = "ABC";

console.log(minWindow(source, target));


console 命令行工具 X clear

                    
>
console