Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class csr1 {
- //csr1:given a string S and an Integer n <= the length of S,
- //generate all possible permutations of length n from the given string S.
- static String s ;
- static char[] c ;
- static int v = 0 ;
- public static void permutations(String s, int n){
- csr1.s = s ;
- c = new char[n];
- v = 0;
- perm(0);
- }
- public static void perm(int n){
- if (n>=c.length){
- System.out.println(c);
- }
- else for (int i = 0; i < s.length(); i++) {
- if((v&(1<<i))==0){
- c[n] = s.charAt(i);
- v|=(1<<i);
- perm(n+1);
- v&=(~(1<<i));
- }
- }
- }
- public static void main(String[] args) {
- permutations("abc",2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement