import java.util.*;
class Solution {
public int solution(String str1, String str2) {
int answer = 0;
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
// 1.두 문자를 2문자씩 쪼개서 교집합, 합집합을 구한다.
// 2.유사도값을 구한다. (교집합/합집합 = 유사도)
// 공백이나 숫자,특수문자가 들어있는 경우는 그 글자 쌍을 버린다.
// 65536을 곱하고 소수점 아래를 버린다.
ArrayList<String> s1 = new ArrayList<>();
for(int i=0; i<str1.length()-1; i++){
String line = str1.substring(i,i+2);
if((line.charAt(0) >= 'a' && line.charAt(0) <= 'z') &&
(line.charAt(1) >= 'a' && line.charAt(1) <= 'z')){
s1.add(line);
}
}
ArrayList<String> s2 = new ArrayList<>();
for(int i=0; i<str2.length()-1; i++){
String line = str2.substring(i,i+2);
if((line.charAt(0) >= 'a' && line.charAt(0) <= 'z') &&
(line.charAt(1) >= 'a' && line.charAt(1) <= 'z')){
s2.add(line);
}
}
ArrayList<String> intersection = new ArrayList<>();
ArrayList<String> union = new ArrayList<>();
//합,교 구하기
for(String x1 : s1){
if(s2.contains(x1)){
intersection.add(x1);
s2.remove(x1);
}
union.add(x1);
}
for(String x2 : s2){
union.add(x2);
}
double num = 0;
if(union.size()==0) num = 1;
else num = (double) intersection.size() / (double) union.size();
return (int) (num * 65536);
}
}
아 카카오는 문제를 진짜 귀찮게 낸다. 그냥 푸는법이없네. 문제 자체가 어렵다기 보다는 그냥 풀기 까다롭고 귀찮고 문제 읽는데 시간이 오래걸린다. 별로 안풀고싶다.
일단 내가 막혔던 부분은 공백,특수문자,숫자가 포함되면은 그 쌍은 버려야한다는 조건에서 살짝 헷갈렸다. 그냥 문자인 것만 포함시키면 되는 거였다. 나머지는 어렵지 않게 해결했다.
substring을 이용해서 문자를 2개씩 쪼개줬으며,
(line.charAt(0) >= 'a' && line.charAt(0) <= 'z') &&
(line.charAt(1) >= 'a' && line.charAt(1) <= 'z')
이 조건으로 문자만 있는걸로 걸러냈다.
저도 개발자인데 같이 교류 많이 해봐요 ㅎㅎ! 서로 화이팅합시다!