본문 바로가기

Algorithm/Algorithm Test

[프로그래머스] 뉴스 클러스터링 (Java)

카테고리

리스트

나만의 카테고리

로직 짜기

문제 링크

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

요점

  • 중복이 허용되는 합집합, 교집합 참고
  • int, double 과 같은 자료형 연산 주의 해야한다
  • 정규식 사용법에 대해서 알아야 한다
  • Java에서 리스트를 사용할 때 복사 방식에 유의 해야 한다. (Call by value VS Call by reference)

참고 지식

  • List 메서드
  • 정규식

풀이 (Java)

import java.util.*;
import java.util.regex.*;

class Solution {
    public int solution(String str1, String str2) {
        int answer = 0;
        
        str1 = str1.toLowerCase();
        str2 = str2.toLowerCase();
        
        List<String> multipleSet1 = getMultipleSetList(str1);
        List<String> multipleSet2 = getMultipleSetList(str2);
        
        List<String> multipleWithProcess1 = removeWithoutAlphabet(multipleSet1);
        List<String> multipleWithProcess2 = removeWithoutAlphabet(multipleSet2);
        
        List<String> intersectionList = getIntersectionList(multipleWithProcess1, multipleWithProcess2);
        List<String> unionList = getUnionList(multipleWithProcess1, multipleWithProcess2);
        
        double intersectionSize = (double)intersectionList.size();
        double unionSize = (double)unionList.size();
        
        if (unionSize == 0) {
            answer = 65536;
        } else {
            answer = (int)(intersectionSize / unionSize * 65536.0);
        }
        
        return answer;
    }
    
    public List<String> removeWithoutAlphabet(List<String> targetList) {
        List<String> arrayList = new ArrayList<String>();
        
        for (String str: targetList) {
            boolean regex = Pattern.matches("[a-zA-Z]*$", str);
            
            if (regex) {
                arrayList.add(str);
            }
        }
        
        return arrayList;
    }
    
    public List<String> getMultipleSetList(String target) {
        List<String> multipleSetList = new ArrayList<String>();
        
        for (int i = 0; i < target.length() - 1; i++) {
            multipleSetList.add(target.substring(i, i+2));
        }
        
        return multipleSetList;
    }
    
    public List<String> getIntersectionList(List<String> a1, List<String> a2) {
        List<String> intersectionList = new ArrayList<>();
        List<String> tempList = new ArrayList<>(a2);
        
        for (String str: a1) {
            if (tempList.remove(str)) {
                intersectionList.add(str);
            }
        }
        
        return intersectionList;
    }
    
    public List<String> getUnionList(List<String> a1, List<String> a2) {
        List<String> unionList = new ArrayList<String>();
        List<String> tempList = new ArrayList<>(a2);
        
        for (String str: a1) {
            tempList.remove(str);
            unionList.add(str);
        }
        
        for (String str: tempList){
            unionList.add(str);
        }
        
        return unionList;
    }
}

 

solution 함수 내에서 모두 처리하도록 코드를 짠다면 훨씬 공간 효율성, 시간 효율성이 좋게 구성 할 수 있을것 같습니다. 하지만 저는 문제에서 각 로직들이 순서에 따라 모듈적으로 동작한다고 느껴져서 각각 함수로 빼서 구성했습니다. 

 


알고리즘 초보가 정리한 글입니다

더 좋은 방법이나 코드에 대한 코멘트 언제나 환영합니다!

 

반응형