728x90
SMALL
문제 이해
- 자카드 유사도를 구하고 65536을 곱한 수를 출력해라
- 자카드 유사도는 두 배열의 교집합/합집합이다.
- 문자열을 2글자씩 잘라서 넣는데 영문이 아니라면 그 문자는 삭제한다.
- 교집합을 구할때는 두 배열을 비교하면서 사용한 index인지 check를 통해 확인후 교집합의 개수를 구한다.
- double 형태로 자카드 유사도가 나오기 때문에 cnt + 0.0을 함으로 형 변환후 자카드 유사도를 구한다.
import java.util.*;
class Solution {
public int solution(String str1, String str2) {
int answer = 0;
str1 = str1.toUpperCase();
str2 = str2.toUpperCase();
List<String> str1List = new ArrayList();
List<String> str2List = new ArrayList();
int cnt = 0;
for(int i = 0; i<str1.length()-1; i++){
String temp = str1.substring(i,i+2);
if(temp.matches("[a-zA-Z]+")){
str1List.add(temp);
}
}
for(int i = 0; i<str2.length()-1; i++){
String temp = str2.substring(i,i+2);
if(temp.matches("[a-zA-Z]+")){
str2List.add(temp);
}
}
// 교집합 구하기
boolean[] check = new boolean[str2List.size()];
for(int i = 0; i<str1List.size(); i++){
for(int j = 0; j<str2List.size(); j++){
if(str1List.get(i).equals(str2List.get(j)) && !check[j]){
check[j] = true;
cnt++;
break;
}
}
}
if(str1List.size() + str2List.size() == 0){
return 65536;
}
Double db = (cnt+0.0) / (str1List.size() + str2List.size() - cnt);
answer = (int) (db * 65536);
return answer;
}
}
728x90
LIST
'자바 > 코딩테스트' 카테고리의 다른 글
[프로그래머스] [3차] 압축 - Java(자바) (1) | 2024.03.29 |
---|---|
[프로그래머스] k진수에서 소수 개수 구하기 - Java(자바) (0) | 2024.03.28 |
[프로그래머스] 피로도 - Java(자바) (0) | 2024.03.28 |
[프로그래머스] 튜플 - Java(자바) (0) | 2024.03.27 |
[프로그래머스] [1차] 캐시 - Java(자바) (0) | 2024.03.27 |