주어진 words
배열이 연속적으로 존재하는 s
문자열의 인덱스를 구하는 문제
/**
* @param {string} s
* @param {string[]} words
* @return {number[]}
*/
var findSubstring = function (s, words) {
let answer = []
const size = words[0].length
const totalSize = words.reduce((total, cur) => total + cur.length, 0)
const play = (str, words) => {
if (!words.length) return true
const word = str.substring(0, size)
const wordIdx = words.indexOf(word)
if (!!~wordIdx) {
words.splice(wordIdx, 1)
return play(str.substring(size), words)
}
return false
}
for (let i = 0; i <= s.length - totalSize; i++) {
if (play(s.substring(i), [...words])) answer.push(i)
}
return answer
}