Hi this is a basic question I encountered on my job interview, I'm trying to get all the permutation of an input string using Java, unfortunately I can't get this to work.
import java.util.Scanner;
public class Test2 {
static void permute(char[] x, int y){
if (y == x.length)
{
for(int i = 0; i < x.length; i++){
System.out.print(x[y]);
}
}
else {
for (int i = y; i < x.length;i++)
{
char temp = x[y];
x[y] = x[i];
x[i] = temp;
permute(x, y + 1);
temp = x[y];
x[y] = x[i];
x[i] = temp;
}
}
}
public static void main(String [] Args){
Scanner scan = new Scanner (System.in);
System.out.println("Input any word :");
String word = scan.nextLine();
int n = word.length();
char [] sequence = new char[n];
for (int i = 0; i < n ; i++)
sequence[i] = scan.next().charAt(0);
System.out.println("These are the permutations: ");
permute(sequence,0);
}
}