Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
Leetcode: Substring with Concatenation of All Words

I'm trying to learn sliding window pattern. I came across this problem in leetcode https://leetcode.com/problems/substring-with-concatenation-of-all-words/

I was able to come up with this answer, which only satisfies some of cases but not the other. So i'm clearly missing something in my algorithm.

Here is the code snippet.


const assert = require('assert');

function substringWithConcatenationOfAllwords(input, pattern) {

  let patternMap = {};
  let windowLen = 0;
  for (let i = 0; i < pattern.length; i++) {
    for (let j = 0; j < pattern[i].length; j++) {
      const char = pattern[i][j];
      if (!(char in patternMap)) {
        patternMap[char] = 0;
      }
      patternMap[char] += 1;
      windowLen += 1;
    }
  }

  const wordLen = pattern[0].length;
  let i = 0;
  let start = 0;
  let indices = [];

  while (i < input.length) {
    const char = input[i];
    if (char in patternMap) {
      patternMap[char] -= 1;
    }

    // once end reaches the total length of pattern map array words
    // start checking for matches and shrink the window
    if (i - start >= windowLen - 1) {

      if (Object.values(patternMap).every(val => val === 0)) {
        indices.push(start);
      }

      let j = 0;

      while (j < wordLen) {
        const startChar = input[start];
        if (startChar in patternMap) {
          patternMap[startChar] += 1;
        }
        start += 1;
        j++;
      }

    }

    i++;
  }

  return indices;
}

assert.deepEqual(
  substringWithConcatenationOfAllwords("barfoothefoobarman", ["foo", "bar"]),
  [0, 9]
);

assert.deepEqual(
  substringWithConcatenationOfAllwords("wordgoodgoodgoodbestword", ["word", "good", "best", "word"]),
  []
);

assert.deepEqual(
  substringWithConcatenationOfAllwords("barfoofoobarthefoobarman", ["bar", "foo", "the"]),
  [6, 9, 12]
);

assert.deepEqual(
  substringWithConcatenationOfAllwords("lingmindraboofooowingdingbarrwingmonkeypoundcake", ["fooo", "barr", "wing", "ding", "wing"]),
  [13]
);

The first 3 cases passes, but the final one didn't pass. Based on the solutions provided in leetcode i see all of them are using word counting, if that is correct way to solve i'm planning to learn that, but before that i want to understand what is the issue with my solution. Can i make my solution work for all cases ?

When i debugged the final case this is how window shrink will happen inside the second while loop


// ling
// mind
// rabo
// ofoo
// owin
// gdin
// gbar
// rwin
// gmon
// keyp
// ound
// cake

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda