Permutations excluding repeated characters

前端 未结 5 1135
無奈伤痛
無奈伤痛 2021-02-06 01:43

I\'m working on a Free Code Camp problem - http://www.freecodecamp.com/challenges/bonfire-no-repeats-please

The problem description is as follows -

<
5条回答
  •  别那么骄傲
    2021-02-06 02:40

    Well I won't have any mathematical solution for you here.

    I guess you know backtracking as I percieved from your answer.So you can use Backtracking to generate all permutations and skipping a particular permutation whenever a repeat is encountered. This method is called Backtracking and Pruning.

    Let n be the the length of the solution string, say(a1,a2,....an). So during backtracking when only partial solution was formed, say (a1,a2,....ak) compare the values at ak and a(k-1). Obviously you need to maintaion a reference to a previous letter(here a(k-1))

    If both are same then break out from the partial solution, without reaching to the end and start creating another permutation from a1.

提交回复
热议问题