const findSubstring = function (s, words) {
if (!s.length || !words.length) return []
const wordMap = new Map()
for (let key of words.values()) {
wordMap.set(key, wordMap.has(key) ? wordMap.get(key) + 1 : 1)
}
const wid = words[0].length
const len = wid * words.length
const result = []
for (let start = 0; start + len <= s.length; start++) {
const tmpMap = new Map()
let j = 0
for (; j < len; j += wid) {
const tmp = s.substr(start + j, wid)
if (wordMap.has(tmp)) {
tmpMap.set(tmp, tmpMap.has(tmp) ? tmpMap.get(tmp) + 1 : 1)
if (tmpMap.get(tmp) > wordMap.get(tmp)) {
break
}
} else {
break
}
}
if (j === len) {
result.push(start)
}
}
return result
}
console.log(findSubstring('aaa', ['a', 'a']))
console.log(findSubstring('foobarfoobar', ['foo', 'bar']))
console.log(findSubstring('barfoothefoobarman', ['foo', 'bar']))