求一个java算法,计算字符串的排序组合方式


public class Composition {
    
    List<String> chars= Arrays.asList("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S"
            ,"T","U","V","W","X","Y","Z");
    // 传入长度n生成n位字符有多少种组合方法 比如传入3 生成abc acd 等等
    public static List<String> generate(int charsLength){
    
    }
}
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Main {
    public static void DFS(List<String> chars, String prefix, int max) {
        if (prefix.length() != 0  && max==0) {
            System.out.println(prefix);
            return;
        }

        for (int i = 0; i < chars.size(); i++) {

            List<String> temp = new LinkedList<String>(chars);
            String item = (String) temp.remove(i); // 取出被删除的元素,这个元素当作一个组合用掉了
            DFS(temp, prefix + item, max - 1);
        }
    }

    static List<String> chars = Arrays.asList("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
            "P",
            "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");

    public static void main(String[] args) {
        DFS(chars, "", 3);
    }

}

算法修改自:https://blog.csdn.net/Mikchy/article/details/91383798

int num = 26;
int sum = 1;
int sum2 = 1;
if(n>26) return 0;
for(int i = n-1;i>0;i--){
sum = sum * (num-i);
sum2 = sum2 * (i+1);
}
return sum/sum2;


private List sort(String input, int charsLength) {

        List sortList = new ArrayList();

        if (charsLength <= 0) {

        System.out.println("提示:您输入了空字符,请输入有效值!");

        return new ArrayList();

        }

        char leftChar = input.charAt(0);

        if (charsLength > 1) {

        String rightString = input.substring(1, charsLength);

        List rightStringSortedList = sort(rightString, charsLength);

        rightStringSortedList.forEach((e) -> {

        for (int i = 0; i < e.length() + 1; i++) {

        sortList.add(new StringBuffer(e).insert(i, leftChar).toString());

        }

        });

        } else {

        sortList.add(String.valueOf(leftChar));

        }

        return sortList;

        }
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632

 // 传入长度n生成n位字符有多少种组合方法 比如传入3 生成abc acd 等等
    public static List<String> generate(int charsLength, List<String> chars) {
        List<String> chars2 = Arrays.asList("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S"
                , "T", "U", "V", "W", "X", "Y", "Z");
        List<String> chars3 = new ArrayList<>(chars);
        for (String string2 : chars2) {
            chars.add(string2);
            for (String string : chars3) {
                chars.add((string + string2));
            }
        }
        if (chars.get(chars.size()-1).length() >=charsLength) {
            chars3 = new ArrayList<>(chars);
            for (String string:chars3){
                if (string.length()!=charsLength){
                    chars.remove(string);
                }
            }
            return chars;
        } else {
            generate(charsLength--,chars);
        }
        return chars;
    }

    public static void main(String[] args) {
        List<String> chars2 = new ArrayList<>();
        generate(1, chars2);
        System.out.println(chars2);
        System.out.println(chars2.size());
    }