헝D의 일기장

https://school.programmers.co.kr/learn/courses/30/lessons/17677

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

나의풀이

import java.util.*;
class Solution {
    public int solution(String str1, String str2) {
        str1 = str1.toLowerCase();
        str2 = str2.toLowerCase();
        
        Map<String, Integer> map1 = new HashMap<>();
        Map<String, Integer> map2 = new HashMap<>();
        Set<String> set = new HashSet<>();
        
        for(int i=0; i<str1.length()-1 ; i++){
            String temp = str1.substring(i, i+2);
            if(Character.isAlphabetic(temp.charAt(0))&&Character.isAlphabetic(temp.charAt(1))){
                map1.put(temp, map1.getOrDefault(temp,0)+1);
                set.add(temp);
                
            }
        }
        
          for(int i=0; i<str2.length()-1 ; i++){
            String temp = str2.substring(i, i+2);
            if(Character.isAlphabetic(temp.charAt(0))&&Character.isAlphabetic(temp.charAt(1))){
                map2.put(temp, map2.getOrDefault(temp,0)+1);
                set.add(temp);
                
            }
        }
        
        //합
        int total=0;
        for(String s: set){
            total+=Math.max(map1.getOrDefault(s,0), map2.getOrDefault(s,0));
        }
        
        //교
        int answer=0;
        for(String s: map1.keySet()){
            if(map2.containsKey(s)){
                answer+=Math.min(map1.get(s), map2.get(s));
            }
        }

        if(total == 0 && answer == 0) return 65536;
     
        return (int)((double)answer/total* 65536)  ;
    }
}
profile

헝D의 일기장

@헝D

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!