본문 바로가기
알고리즘/프로그래머스

뉴스 클러스터링 (자바)

by 김어찐 2021. 9. 12.
728x90
package prog;

import java.util.HashMap;

public class 뉴스클러스터링 {
    public static void main(String[] args) {
        Solution_뉴스클러스터링 s = new Solution_뉴스클러스터링();
        System.out.println(s.solution("FRANCE",	"french"));
    }
}
class Solution_뉴스클러스터링{
    public int solution(String str1, String str2) {
        int answer = 0;
        HashMap<String, Integer> map1 = new HashMap<>();
        HashMap<String, Integer> map2 = new HashMap<>();

        makeMultiMap(map1,str1);
        makeMultiMap(map2,str2);

        HashMap<String, Integer> sumMap = new HashMap<>();
        HashMap<String, Integer> diffMap;

        makeSumMap(map1, sumMap);
        makeSumMap(map2, sumMap);

        diffMap =makeDiffMap(map1, map2);

        int sumCount = getCount(sumMap);
        int diffCount = getCount(diffMap);
        if (sumCount == 0) {
            return 65536;
        }
        answer = (int)((double)diffCount/sumCount*65536);
        return answer;
    }

    private int getCount(HashMap<String, Integer> map) {
        int count=0;
        for (String key : map.keySet()) {
            count+= map.get(key);
        }
        return count;
    }

    private HashMap<String, Integer> makeDiffMap(HashMap<String, Integer> map1,HashMap<String, Integer> map2) {
        HashMap<String, Integer> diffMap = new HashMap<>();
        for (String key : map1.keySet()) {
            if(map2.containsKey(key)){
                diffMap.put(key, Math.min(map1.get(key), map2.get(key)));
            }
        }
        return diffMap;
    }


    private void makeSumMap(HashMap<String, Integer> map,HashMap<String, Integer> sumMap ) {

        for (String key : map.keySet()) {
            if(sumMap.containsKey(key)){
                sumMap.put(key, Math.max(map.get(key), sumMap.get(key)));
            }
            else sumMap.put(key, map.get(key));

        }

    }

    public void makeMultiMap(HashMap<String,Integer> map, String str){
        for (int i = 0; i < str.length()-1; i++) {
            String tmpStr = str.substring(i,i+2);
            tmpStr = tmpStr.toUpperCase();
            if (charCheck(tmpStr.charAt(0)) && charCheck(tmpStr.charAt(1))) {
                if (map.containsKey(tmpStr)) {
                    map.put(tmpStr, map.get(tmpStr) + 1);
                }
                else map.put(tmpStr, 1);
            }
        }
    }

    public boolean charCheck(char c){
        if('A'<=c && c<='Z'){
            return true;
        }
        return false;

    }
}
728x90

'알고리즘 > 프로그래머스' 카테고리의 다른 글

행렬 테두리 회전 (자바)  (0) 2021.09.23
5주차 모음사전 (자바)  (0) 2021.09.14
표편집 (자바)  (0) 2021.09.12
짝지어 제거하기 (자바)  (0) 2021.09.09
더 맵게 (자바)  (0) 2021.09.08