Saturday 31 March 2012

Permutations of a string in java

Print all permutations of a string in java:


public static void permutate(String o, String t,boolean used[]){
               if(t.length()==o.length()){
                       System.out.println(t);return;
               }
               boolean used2[] = new boolean[o.length()];
               for(int i=0;i<o.length();i++){
                       if(used[i])continue;
                       System.arraycopy(used, 0, used2, 0, o.length());
                       used2[i] = true;
                       permutate(o,t+o.charAt(i),used2);
               }
       }

No comments:

Post a Comment