Finding all the unique permutations of a string without generating duplicates

前端 未结 5 1613
轻奢々
轻奢々 2020-11-28 10:03

Finding all the permutations of a string is by a well known Steinhaus–Johnson–Trotter algorithm. But if the string contains the repeated characters such as
AABB,
the

5条回答
  •  暖寄归人
    2020-11-28 10:25

    Use the following recursive algorithm:

    PermutList Permute(SymArray fullSymArray){
        PermutList resultList=empty;
        for( each symbol A in fullSymArray, but repeated ones take only once) {
           PermutList lesserPermutList=  Permute(fullSymArray without A)
           for ( each SymArray item in lesserPermutList){
                resultList.add("A"+item);
           }
        }
        return resultList;
    }
    

    As you see, it is very easy

提交回复
热议问题