Advertisement
leonardo_aly7

csr1

Jan 29th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1.  
  2. public class csr1 {
  3.  
  4.    
  5.     //csr1:given a string S and an Integer n <= the length of S,
  6.     //generate all possible permutations of length n from the given string S.
  7.  
  8.     static String s ;
  9.     static char[] c ;
  10.     static int v  = 0 ;
  11.    
  12.     public static void permutations(String s, int n){
  13.         csr1.s = s ;
  14.         c = new char[n];
  15.         v = 0;
  16.         perm(0);
  17.     }
  18.    
  19.     public static void perm(int n){
  20.         if (n>=c.length){  
  21.             System.out.println(c);
  22.            
  23.         }
  24.         else for (int i = 0; i < s.length(); i++) {
  25.             if((v&(1<<i))==0){
  26.             c[n] = s.charAt(i);
  27.             v|=(1<<i);
  28.             perm(n+1);
  29.             v&=(~(1<<i));
  30.             }
  31.         }
  32.     }
  33.    
  34.     public static void main(String[] args) {
  35.         permutations("abc",2);
  36.  
  37.     }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement