[笔试编程-手撕代码]给定一个只包含大写英文字母的字符串s,求对s重新排列的所有不同的排列数,包含该输入的字符串本身

给定一个任意一个字符串s,求重新排列的所有不同的排列数,包含该输入的字符串本身。
[笔试编程-手撕代码]给定一个只包含大写英文字母的字符串s,求对s重新排列的所有不同的排列数,包含该输入的字符串本身_第1张图片


import java.util.*;
/**
 * @PackageName: PACKAGE_NAME
 * @ClassName: Test10
 * @Author: 
 * @Date: 2020/4/16 20:20
 * @Description: //TODO
 */
public class Test10 {
     
    public static void main(String[] args) {
     
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
     
            String s = sc.nextLine().toUpperCase();
            if (!s.isEmpty()) {
     
                int length = s.length();
                char[] chars = s.toCharArray();
                Arrays.sort(chars);
                Map<Character, Integer> map = new HashMap<>();
                for (int i = 0; i < chars.length; i++) {
     
                    boolean flag = true;
                    int num = 1;//该字母的个数;
                    while (flag) {
     
                        if (i+1<chars.length &&chars[i] == chars[i + 1]) {
     
                            if (!(i+1< chars.length)){
     
                                break;
                            }
                            i++;
                            num++;
                        } else {
     
                            flag = false;
                            map.put(chars[i], num);
                        }
                    }
                }
                Collection<Integer> values = map.values();
                long pailieshu = getJieChen(length);
                for (int chushu : values) {
     
                    pailieshu = pailieshu / getJieChen(chushu);
                }
                System.out.println(pailieshu);
            }
        }
    }

    public static long getJieChen(int num) {
     
        long result = 1;
        for (int i = 0; i < num; i++) {
     
            result = result * (num-i) ;
        }
        return result;
    }
}

测试结果:

D:\developer\Java\jdk1.8.0_231\bin\java.exe -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:56798,suspend=y,server=n -javaagent:C:\Users\changlei\.IntelliJIdea2019.1\system\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "D:\developer\Java\jdk1.8.0_231\jre\lib\charsets.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\deploy.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\ext\access-bridge-64.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\ext\cldrdata.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\ext\dnsns.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\ext\jaccess.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\ext\jfxrt.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\ext\localedata.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\ext\nashorn.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\ext\sunec.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\ext\sunjce_provider.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\ext\sunmscapi.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\ext\sunpkcs11.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\ext\zipfs.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\javaws.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\jce.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\jfr.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\jfxswt.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\jsse.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\management-agent.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\plugin.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\resources.jar;D:\developer\Java\jdk1.8.0_231\jre\lib\rt.jar;D:\developer\IdeaProjects\offer\out\production\fuxi;D:\developer\JetBrains\IntelliJ IDEA 2019.1\lib\idea_rt.jar" Test10
Connected to the target VM, address: '127.0.0.1:56798', transport: 'socket'
ABCDEFGHHA
907200
ABA
3
A
1
AA
1
AAAAAA
1
AAAAAB
6
ABC
6
ABCD
24

我写的代码对任意一个字符串求所有不同的排列数都适用吧?

你可能感兴趣的:(机试题,字符串,java,算法)