Generate list of all possible permutations of a string

后端 未结 30 2544
故里飘歌
故里飘歌 2020-11-22 15:10

How would I go about generating a list of all possible permutations of a string between x and y characters in length, containing a variable list of characters.

Any l

30条回答
  •  囚心锁ツ
    2020-11-22 15:21

    import java.util.*;
    
    public class all_subsets {
        public static void main(String[] args) {
            String a = "abcd";
            for(String s: all_perm(a)) {
                System.out.println(s);
            }
        }
    
        public static Set concat(String c, Set lst) {
            HashSet ret_set = new HashSet();
            for(String s: lst) {
                ret_set.add(c+s);
            }
            return ret_set;
        }
    
        public static HashSet all_perm(String a) {
            HashSet set = new HashSet();
            if(a.length() == 1) {
                set.add(a);
            } else {
                for(int i=0; i

提交回复
热议问题