Generating all permutations of a given string

前端 未结 30 1713
我寻月下人不归
我寻月下人不归 2020-11-21 06:35

What is an elegant way to find all the permutations of a string. E.g. permutation for ba, would be ba and ab, but what about longer st

30条回答
  •  面向向阳花
    2020-11-21 06:37

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Scanner;
    public class hello {
        public static void main(String[] args) throws IOException {
            hello h = new hello();
            h.printcomp();
        }
          int fact=1;
        public void factrec(int a,int k){
            if(a>=k)
            {fact=fact*k;
            k++;
            factrec(a,k);
            }
            else
            {System.out.println("The string  will have "+fact+" permutations");
            }
            }
        public void printcomp(){
            String str;
            int k;
            Scanner in = new Scanner(System.in);
            System.out.println("enter the string whose permutations has to b found");
            str=in.next();
            k=str.length();
            factrec(k,1);
            String[] arr =new String[fact];
            char[] array = str.toCharArray();
            while(p= k-1)
                    y=y-(k-1);
                else
                    y++;
            }
            if (g >= k-1)
                g=1;
            else
                g++;
        }
    
    }
    

提交回复
热议问题