三個或三個以上字母的組合

我想刪除單個字符串,并希望保留最小長度為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ù),它將只找到與最小長度匹配的組合:

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"]

主站蜘蛛池模板: 亚洲日本一区二区一本一道| 精品一区二区三区3d动漫| 亚洲国产AV无码一区二区三区| 精品国产一区二区三区久久影院| 亚洲一区二区影院| 久久精品国产免费一区| A国产一区二区免费入口| 亚洲第一区在线观看| 日韩av片无码一区二区不卡电影| 亚洲午夜一区二区电影院| 亚洲线精品一区二区三区| 日韩精品无码一区二区三区四区| 变态拳头交视频一区二区| 国产亚洲福利精品一区二区| 秋霞日韩一区二区三区在线观看| 风间由美性色一区二区三区| 久久伊人精品一区二区三区| 国产视频一区二区在线播放| 99精品国产高清一区二区三区| 国产成人综合精品一区| 国产日韩高清一区二区三区 | 狠狠爱无码一区二区三区| 麻豆精品一区二区综合av| 欧洲精品免费一区二区三区 | 国产无线乱码一区二三区| 国产伦精品一区二区三区视频金莲| 视频一区二区在线观看| 国产一区二区三区四| 亚洲A∨精品一区二区三区| 一区二区三区在线| 亚洲综合在线成人一区| 91久久精品国产免费一区| 在线观看一区二区三区av| 无码人妻精一区二区三区 | 中文字幕一区二区免费| 成人区精品一区二区不卡 | 亚洲无删减国产精品一区| 麻豆一区二区在我观看| 亚洲熟妇AV一区二区三区宅男| 国产乱人伦精品一区二区在线观看| 国内精品一区二区三区最新|