Saturday, August 19, 2017

Permutations of a string example in Java?

Permutation of a string means the possible combinations of corrector's of a given string.

For example given string is "ABC" then possible combinations are "ABC","ACB","BAC","BCA","CAB" and "CBA". Here is the example.

public class permutation {
 
           public static void main(String[] args) {
                 perM("","ABC");
           }

           private static void perM(String bigin,String end) {
  
                 if(end.length()<=1){

                       System.out.println(bigin+end);

                  }else{

                      for(int i=0;i<end.length();i++){

                          String str=end.substring(0,i)+end.substring(i+1);
                         perM(bigin+end.charAt(i),str);

                      }
                }
          }
}

No comments: