Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda