Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!