public static arraylist<string> getperms (String str)
{
if (str==null)
return null;
Arraylist<string> permutations=new arraylist<string> ();
if (Str.length () ==0)//Termination condition
{
Permutations.add ("");
return permutations;
}
Char First=str.charat (0);//Get first character
String remainder=str.substring (1);//Remove First character
Arraylist<string> words=getperms (remainder);
for (String word:words)
{
for (int j=0;j<=word.length (); j + +)
{
String S=insertcharat (WORD,FIRST,J);
Permutations.add (s);
}
}
return permutations;
}
public static string Insertcharat (string Word,char c,int i)
{
String start=word.substring (0,i);
String end=word.substring (i);
return start+c+end;
}
Because there will be n! The time complexity of this solution is O (n!).
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Write a method that determines all permutation combinations of a string