publicstaticvoidmain(String[] args){
Scanner scanner =newScanner(System.in);
String s = scanner.nextLine();allSortCount(s);}staticvoidallSortCount(String s){
char[] chars = s.toCharArray();
List<Character> charList =newArrayList<>(chars.length);for(char c : chars){
charList.add(c);}// 求字符串长度的阶乘(此时是全排列的个数,未去重)
long count =jc(s.length());// 求重复元素的个数,只需要有重复的,例 aaabbbccde,最终得到 [3,3,2]
Collection<Long> collect = charList.stream().collect(Collectors.groupingBy(Character::charValue, Collectors.counting())).values();
collect.removeIf(e -> e <1);// 重复元素个数的阶乘的乘积,(3*2*1)*(3*2*1)*(2*1)
long dumpCount =1;for(Long value : collect){
dumpCount = dumpCount *jc(value.intValue());}
System.out.println("去重后的全排列个数:"+ count / dumpCount);}static int jc(int num){if(num <=1){return1;}
int result = num *jc(num -1);return result;}
未去重的全排列,想去重可以添加到一个set中(网上抄的,只记录一下)
publicstaticvoidmain(String[] args){
Scanner scanner =newScanner(System.in);
String s = scanner.nextLine();sort(s.toCharArray(),0, s.length()-1);}staticvoidsort(char[] arr, int begin, int end){if(begin == end){
System.out.println(Arrays.toString(arr));return;}for(int i = begin; i <= end; i++){swap1(arr, begin, i);sort(arr, begin +1, end);swap1(arr, begin, i);}}staticvoidswap1(char[] arr, int a, int b){
char temp = arr[a];
arr[a]= arr[b];
arr[b]= temp;}