share

Saturday, 17 January 2015

Permutation of a number

Find all permutation of a number
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 swap[j]
               char ch=str[i];
               str[i]=str[j];
               str[j]=ch;
               doPermutation(str, i+1, n);
               // swap str[i] and swap[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));       
    }
   
}
==========================
Output:
abc
acb
bac
bca
cba
cab






 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