I need to print all possible combination of any String without any repetition of character,
for Example suppose input is-ABC
desired combinations are-ABC
ACB
BCA
BAC
CAB
CBA
Below is my code which is running fine-
public static void main(String[] args) {
String name="abcd";
char[] nameArray=name.toCharArray();
String result = null;
int flag=0;
for(int i=0;i<nameArray.length;i++){
for(int j=0;j<nameArray.length;j++){
for(int k=0;k<nameArray.length;k++){
for(int l=0;l<nameArray.length;l++){
result=""+nameArray[i]+nameArray[j]+nameArray[k]+nameArray[l];
flag=0;
for(Character c:nameArray){
if(!result.contains(c.toString())){
flag=1;
}
}
if( flag==0){
System.out.println(result);
}
}
}
}
}
}
problem with this is this is a static,like- we are using 4 for loop since length of String is 4.How to write same code for dynamic String of various length? also its time complexity is very high(n^n),but this is not such major issue.