var findSubstring = function (s, words) { const wordLength = words[0].length const matchLength = wordLength * words.length const wordsCount = {} for (const item of words) { if (!wordsCount[item]) { wordsCount[item] = 0 } ++wordsCount[item] } const result = [] for (let startIndex = 0; startIndex < wordLength; ++startIndex) { let checkMap = { ...wordsCount } let left = startIndex let right = startIndex while (right + wordLength <= s.length) { const curWord = s.substring(right, right + wordLength) right += wordLength if (checkMap[curWord] === undefined) { checkMap = { ...wordsCount } left = right continue } if (checkMap[curWord] === 0) { while (s.substring(left, left + wordLength) !== curWord) { left += wordLength } left += wordLength right = left checkMap = { ...wordsCount } continue } --checkMap[curWord] if (right - left === matchLength) { result.push(left) ++checkMap[s.substring(left, left + wordLength)] left += wordLength } } } return result }