字母与三个或更多字母的组合
combination of letters with three and more letters
我想删除单个字符串并希望保留最小长度为 3 及以上的字符串我尝试使用此 if(result.string >= 3) 访问字符串但它给出数组长度所以我尝试访问字符串,但我不能。所以请任何可以帮助我的人
let stringCombinations = (str) => {
let strLength = str.length;
let result = [];
let currentIndex = 0;
while (currentIndex < strLength) {
let char = str.charAt(currentIndex);
let x;
let arrTemp = [char];
for (x in result) {
arrTemp.push("" + result[x] + char);
}
result = result.concat(arrTemp);
currentIndex++;
}
return result;
};
console.log(stringCombinations("BLADE"));
This is my ouput
output: (31) ["B", "L", "BL", "A", "BA", "LA", "BLA", "D", "BD", "LD", "BLD", "AD", "BAD", "LAD", "BLAD", "E", "BE", "LE", "BLE", "AE", "BAE", "LAE", "BLAE", "DE", "BDE", "LDE", "BLDE", "ADE", "BADE", "LADE", "BLADE"]
This is what I want
["BLA", "BLD", "BAD", "LAD", "BLAD", "BLE", "BAE", "LAE", "BLAE", "BDE", "LDE", "BLDE", "ADE", "BADE", "LADE", "BLADE"]
您想filter your array using the string length:
["B", "L", "BL", "A", "BA", "LA", "BLA", "D", "BD", "LD", "BLD", "AD", "BAD", "LAD", "BLAD", "E", "BE", "LE", "BLE", "AE", "BAE", "LAE", "BLAE", "DE", "BDE", "LDE", "BLDE", "ADE", "BADE", "LADE", "BLADE"]
.filter(s => s.length >= 3)
.forEach(s => console.log(s))
如 @shabs 回答中所述,您可以过滤输出数组以满足您的需要。
但是,这里有一个函数,它只会找到匹配最小长度的组合:
function stringCombinations(str, length) {
let results;
if (length == 1)
results = str.split(""); // Append every character
else
results = [];
for (let i = 0; i < str.length; i++) {
let startChar = str.charAt(i);
let remainingStr = str.substring(i + 1);
let subCombinations = stringCombinations(remainingStr, Math.max(1, length - 1));
subCombinations.forEach(combination => {
results.push(startChar + combination);
})
}
return results;
}
对于"ABC", 2
作为输入,它会给你["AB", "AC", "ABC", "BC"]
我想删除单个字符串并希望保留最小长度为 3 及以上的字符串我尝试使用此 if(result.string >= 3) 访问字符串但它给出数组长度所以我尝试访问字符串,但我不能。所以请任何可以帮助我的人
let stringCombinations = (str) => {
let strLength = str.length;
let result = [];
let currentIndex = 0;
while (currentIndex < strLength) {
let char = str.charAt(currentIndex);
let x;
let arrTemp = [char];
for (x in result) {
arrTemp.push("" + result[x] + char);
}
result = result.concat(arrTemp);
currentIndex++;
}
return result;
};
console.log(stringCombinations("BLADE"));
This is my ouput
output: (31) ["B", "L", "BL", "A", "BA", "LA", "BLA", "D", "BD", "LD", "BLD", "AD", "BAD", "LAD", "BLAD", "E", "BE", "LE", "BLE", "AE", "BAE", "LAE", "BLAE", "DE", "BDE", "LDE", "BLDE", "ADE", "BADE", "LADE", "BLADE"]
This is what I want
["BLA", "BLD", "BAD", "LAD", "BLAD", "BLE", "BAE", "LAE", "BLAE", "BDE", "LDE", "BLDE", "ADE", "BADE", "LADE", "BLADE"]
您想filter your array using the string length:
["B", "L", "BL", "A", "BA", "LA", "BLA", "D", "BD", "LD", "BLD", "AD", "BAD", "LAD", "BLAD", "E", "BE", "LE", "BLE", "AE", "BAE", "LAE", "BLAE", "DE", "BDE", "LDE", "BLDE", "ADE", "BADE", "LADE", "BLADE"]
.filter(s => s.length >= 3)
.forEach(s => console.log(s))
如 @shabs 回答中所述,您可以过滤输出数组以满足您的需要。
但是,这里有一个函数,它只会找到匹配最小长度的组合:
function stringCombinations(str, length) {
let results;
if (length == 1)
results = str.split(""); // Append every character
else
results = [];
for (let i = 0; i < str.length; i++) {
let startChar = str.charAt(i);
let remainingStr = str.substring(i + 1);
let subCombinations = stringCombinations(remainingStr, Math.max(1, length - 1));
subCombinations.forEach(combination => {
results.push(startChar + combination);
})
}
return results;
}
对于"ABC", 2
作为输入,它会给你["AB", "AC", "ABC", "BC"]