let see line 81,the value of words is ‘foo-the’
and step next see line 69,split ‘foot-the’ with ‘-‘ ,it turns out ‘the’
the problem comes from when solving the leetcode algorithm question
description :
You are given a string s and an array of strings words of the same length. Return all starting indices of substring(s) in s that is a concatenation of each word in words exactly once, in any order, and without any intervening characters.
You can return the answer in any order.
full code as follows:
var s = "barfoofoobarthefoobarman"
var words = ["bar", "foo", "the"]
var arr = []
var position = []
function dfs(index, temp, words) {
var wordsP = words.split('-')
if (wordsP.length) {
temp = temp.concat(wordsP[index])
wordsP.splice(index, 1)
}
if (!wordsP.length) {
arr.push(temp)
return;
}
for (var i = 0; i < words.length; i++) {
dfs(i, temp, wordsP.join('-'))
}
}
for (k = 0; k < words.length; k++) {
dfs(k, '', words.join('-'))
}
//
arr.forEach((item) => {
if (s.indexOf(item) != -1) {
position.push(s.indexOf(item))
}
})