首页 > 2021.9.5-度小满-java后端-笔试
头像
我不是坤坤
发布于 2021-09-05 16:49
+ 关注

2021.9.5-度小满-java后端-笔试

//第一题(注解版)
//这个题的主要思想就是两个字符串找不同,然后减减减。所以选用两个hashmap。

import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;

public class 公司名称 {
    public static int [] test(String s,String s2) {
        HashMap<Character, Integer> hashMap1 = new HashMap<>(); // 对应s
        HashMap<Character, Integer> hashMap2 = new HashMap<>(); // s2
        char c1[] = s.toCharArray();
        char c2[] = s2.toCharArray();
                //组建我们的hashmap。
        for (int i = 0; i < c1.length; i++) {
            if (hashMap1.containsKey(c1[i])) {
                int count = hashMap1.get(c1[i]) + 1;
                hashMap1.put(c1[i], count);
            } else {
                hashMap1.put(c1[i], 1);
            }
        }
        for (int i = 0; i < c2.length; i++) {
            if (hashMap2.containsKey(c2[i])) {
                int count = hashMap2.get(c2[i]) + 1;
                hashMap2.put(c2[i], count);
            } else {
                hashMap2.put(c2[i], 1);
            }
        }
                
                //set是用来遍历的
        Set<Character> k1 = hashMap1.keySet();
        Set<Character> k2 = hashMap2.keySet();
        int min=Integer.MAX_VALUE;
                //关键点在于这个K,打个比方:minn 和 mmiinnn 所以K倍的n需要在比较的时候除去比例  这里的min就是应该处理几次
        for (Character chars : k1) {
            int k=hashMap1.get(chars);
            if(k>1) min=Math.min(hashMap2.get(chars)/k,min);
            min=Math.min(hashMap2.get(chars),min);
        }
        for (Character chars : k1) {
            if(hashMap2.containsKey(chars)){
                int num = hashMap2.get(chars)-min;
                hashMap2.put(chars,num);
            }else{
                return new int[]{0,0};
            }
        }
        int res=0;
        for (Character chars : k2) {
            if(hashMap2.containsKey(chars)){
                int num=hashMap2.get(chars);
                if(num>0){
                    res=res+1;
                }
            }
        }
        return new int[]{min,res};
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s =sc.nextLine();
        String s2=sc.nextLine();
        int []nums = new int[2];
        nums = test(s,s2);
        System.out.print(nums[0]+" ");
        System.out.print(nums[1]);
    }

}
               

全部评论

(2) 回帖
加载中...
话题 回帖