取这个“ str”“ ABCDE”,它具有以下"AB", "ABC", "ABCD","ABCDE","DE", "CDE", "BCDE", "CD","BC"
有关顺序的组合。我试过了
// thi is javascript code
const val = 'ABCDE'
let array = []
for (i = 0; i < val.length-1; i++) {
array.push(val.slice(i,val.length))
array.push(val.slice(0,i+2))
array.push(val.slice(i,val.length-i))
}
console.log(
array,
array.includes('BC'),
array.includes('CD')
)
// this is the reuslts: ["ABCDE", "AB", "ABCDE", "BCDE", "ABC", "BCD", "CDE", "ABCD", "C", "DE", "ABCDE", ""]
//this reslutes don't have compnations like 'BC' or 'CD'
我没有真正得到所需的结果。
请问您在javascript或python中有任何想法,我认为pythonpanda
或可能是NumPy
库中有这样的东西。
您需要两个嵌套循环用于子字符串。
function getAllSubstrings(str) {
const result = [];
for (let i = 0; i < str.length - 1; i++) {
for (let j = i + 2; j < str.length + 1; j++) {
result.push(str.slice(i, j));
}
}
return result;
}
console.log(getAllSubstrings('ABCDE'));
.as-console-wrapper { max-height: 100% !important; top: 0; }
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句