我想刪除單個字符串,并希望保留最小長度為3及以上的字符串。我嘗試使用if(result.string>=3)訪問字符串,但它給出了數(shù)組長度,所以我嘗試訪問字符串,但我做不到。所以請任何能幫助我的人
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"]
正如@shabs answer中所說,您可以過濾輸出數(shù)組以滿足您的需要。
但是,這里有一個函數(shù),它將只找到與最小長度匹配的組合:
對于
"ABC", 2
作為輸入,它將給您["AB", "AC", "ABC", "BC"]