Stack overflow lors du calcul des permutations d'une chaîne
bonjour
la méthode permutations2 devrait renvoyer toutes les permutations d'une string, mais j'ai une stack overflow error: (pour info la méthode permutations3 marche mais que avec des chaînes ne comportant pas de caractères répétés)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| implicit class utils(val chaîne: String) {
def permutations2: Seq[String] =
{
if(chaîne.size == 1)
Seq(chaîne);
else
chaîne.zipWithIndex.flatMap{case (x:Char,i:Int) => chaîne.drop(i).permutations2.map(x +_)}
}
def permutations3 : Seq[String] =
{
if(chaîne.size == 1)
Seq(chaîne)
else
chaîne.flatMap(x => chaîne.filterNot(_ == x).permutations3.map(x +));
}
} |
pouvez-vous m'aider?