J'essaie de calculer toutes les permutations possibles d'une chaîne donnée en utilisant la récursivité en Java. Cependant, je ne sais pas ce qui ne va pas avec mon code.Calcul récursif de toutes les permutations possibles d'une chaîne Java
Voici mon algorithme:
public static ArrayList<String> computeAllPossiblePermutations(String str)
{
ArrayList<String> perms = new ArrayList<>();
//base case
if (str.length() == 1)
perms.add(str);
else
{
//loop over the string
for (int i = 0; i < str.length() - 1; i++)
{
//make a subset of the string excluding the first char
String sub = str.substring(i+1, str.length());
//compute permutations of the subset
ArrayList<String> subPerms = computeAllPossiblePermutations(sub);
//add the first char that we excluded at the start of each permutations
for (String s : subPerms)
{
s = str.charAt(i) + s;
perms.add(s);
}
}
}
return perms;
}
apprécierait toute aide. Merci!
Nous ne savons pas ce qui ne va pas non plus. Si vous obtenez un mauvais résultat, vous devriez montrer ce que vous attendez de ce que vous obtenez réellement. – Carcigenicate