섹션2. 해싱 & 시간 파싱 알고리즘 - (2)

728x90

🎈섹션2. 해싱 & 시간 파싱 알고리즘 - (2)

🟦 2-3. 서로 다른 빈도수 만들기

문제 풀이

  • 이 문제는 유일한(Unique) 빈도수를 구하는 게 관건이다.
  • 새로운 HashSet 를 만들어서 HashMap에 각 문자별 빈도수만 뽑아서 중복 검사 후 담아야 한다.
  • 1) 각각의 Map에 담긴 전체 key 순회하면서
  • 2) 각 key의 빈도수값이 Set에 중복되는 동안 계속 while() 문 돌며 Map에 다시 해당 key에 대한 value를 -1 처리, answer++ (중복 동안)
  • 3) while 빠져나와서 만약 map의 해당 key 값이 0인 경우 continue
  • 4) 확인용 Set에 최종 add처리한다.

코드

package to_0424;

import java.util.HashMap;
import java.util.HashSet;

public class Solution1_Re {

    //솔루션 함수 
    public int solution(String s) {
        int answer= 0;
        HashMap<Character, Integer> map = new HashMap<>();
        HashSet<Integer> set = new HashSet<>();

        //1) 맵에 우선 중복없이 각 빈도수를 담는다.
        for(char x : s.toCharArray()) {
            map.put(x, map.getOrDefault(x, 0)+1);
        }

        //2) 각 key별 빈도수가 유일한지 확인용 Set에 담기 전 확인하면서 담는다. 
        //Set = 중복 불허 자료구조
        for(char key : map.keySet()) {
            //Set에 이미 map의 빈도수 중복 값 있는 동안 반복할 거임
            while(set.contains(map.get(key))) {
                //중복되는 동안. 해당 키의 빈도수값 -1처리 
                map.put(key, map.get(key)-1);
                answer++;
            }
            //탈출 후 만약 해당 빈도수 0 되면 별다르 처리없이 continue처리
            if(map.get(key) == 0) continue;

            set.add(map.get(key));//Set에 중복되지 않는 빈도수를 담는다. 
        }

        return answer;
    }

    //실행 메인 
    public static void main(String[] args){
        Solution1 T = new Solution1();
        System.out.println(T.solution("aaabbbcc"));    
        System.out.println(T.solution("aaabbc"));    
        System.out.println(T.solution("aebbbbc"));    
        System.out.println(T.solution("aaabbbcccde"));    
        System.out.println(T.solution("aaabbbcccdddeeeeeff"));    
    }
}

728x90