[프로그래머스] [1차] 뉴스 클러스터링

2020. 4. 1. 14:33프로그래머스/카카오

import java.util.*;

class Solution {
  public int solution(String str1, String str2) {
      int answer = 0;
      double result = 0;
      List<String> multiset = new ArrayList<>();
      List<String> multiset2 = new ArrayList<>();
      List<String> multisection = new ArrayList<>();
      
      for(int i=0; i<=str1.length()-2; i++){
          
          if (alphabetTest(str1.charAt(i)) && alphabetTest(str1.charAt(i+1))){
            if(i!=str1.length()-2){
              multiset.add(str1.substring(i,i+2));
            }else{
                multiset.add(str1.substring(i));
            }
      }
    }
      
      for(int i=0; i<=str2.length()-2; i++){
           if (alphabetTest(str2.charAt(i)) && alphabetTest(str2.charAt(i+1))){
                if(i!=str2.length()-2){
               multiset2.add(str2.substring(i,i+2));
                }else{
            multiset2.add(str2.substring(i));
                }
      }    
    }
      
      for(int i=0; i<multiset.size(); i++){
          if(checkContains(multiset2,multiset.get(i))){
              multisection.add(multiset.get(i));
          }
      }
      
      if((multiset.size()+multiset2.size()-multisection.size())!=0){
          result = (double)multisection.size()/(double)(multiset.size()+multiset2.size()-multisection.size());
      }else{
          result = 1;
      }
      
      answer = (int)(result * 65536);  
      
      return answer;
  }
    public boolean alphabetTest(char c){
        if( 65<=c && c<=90){
            return true;
        }
        else if( 97<=c && c<=122){
            return true;
        }
        else{
            return false;
        }
    }
    public boolean checkContains(List<String> multiset2, String s){
        for(int i=0; i<multiset2.size(); i++){
            if(multiset2.get(i).equalsIgnoreCase(s)){
                multiset2.set(i,"사용됨");
                return true;
            }
        }
        return false;
    }
}