Wednesday, 21 January 2015

Generate permutation of string

Permutation:
each of several possible ways in which a set or number of things can be ordered or arranged. 

public class permutation {
  void    doPermutation(char [] str,int i,int n){
       if (i==n) {
          for (int m = 0; m < str.length; m++) {
       System.out.print(str[m]);
          }
          System.out.println("");
    
     }
       else{
            for (int j = i; j <=n; j++) {
                //swap str[i] and str[j]
                 char ch=str[i];
                 str[i]=str[j];
                 str[j]=ch;
                 doPermutation(str, i+1, n);
                 //swap str[i] and str[j]
                 ch=str[i];
                 str[i]=str[j];
                 str[j]=ch;
          }
       }
         
     }   
     public static void main(String[] args) {
    
          String str1="abc";
          permutation obj=new permutation();
          char []str=str1.toCharArray();
          obj.doPermutation(str,0,(str.length-1));       
     }
    
}



 Related Posts:


Check a string value is Integer or not in java. (Solution).

Check ia string is hexadecimal number or not (Solution).

Generate Combination in java (Solution).

Generate permutation of string(Solution).

Reverse string in java (Solution).

Find duplicate characters with occurrences in a string(Solution).

Permutation of a number(Soluion).

Split the String in java(Solution).

Convert string into number in java(Solution).

Swap two strings without using any variable(Solution).

Get a number from a string in java(Solution).

Add Two Big Number In Java(Solution).

Expand String(Solution).

Reverse String (Solution).


No comments:

Post a Comment