Array Arrangement Permutation

前端 未结 4 605
半阙折子戏
半阙折子戏 2021-01-26 18:09

Looking for a way to sequentially find different arrangement possibilities of an array. I only care about adding them sequentially, doesn\'t need skip or shuffle values.

4条回答
  •  死守一世寂寞
    2021-01-26 18:16

    You could iterate over each character once and should be able to populate all sequences.

    Here is what you could do.

    var inputArray = ['a', 'b', 'c', 'd', 'e', 'f'];
    
    var outputStrings = [];
    
    inputArray.forEach((item, idx) => {
      let prevString = (idx !== 0) ? outputStrings[idx - 1] : "";
      outputStrings.push(prevString + item);
    });
    
    console.log(outputStrings);

提交回复
热议问题