Skip to content

Instantly share code, notes, and snippets.

@iamchiwon
Created November 5, 2020 14:50
Show Gist options
  • Select an option

  • Save iamchiwon/63d6754db74da5dd274c1ad52c159cbd to your computer and use it in GitHub Desktop.

Select an option

Save iamchiwon/63d6754db74da5dd274c1ad52c159cbd to your computer and use it in GitHub Desktop.
[2018 카카오 공채] 뉴스 클러스터링
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
// https://programmers.co.kr/learn/courses/30/lessons/17677
class Solution {
public int solution(String str1, String str2) {
List<String> s1 = split2(str1);
List<String> s2 = split2(str2);
int n1 = intersectCount(s1, s2);
int n2 = unionCount(s1, s2);
if (n1 == 0 && n2 == 0) return 65536;
return n1 * 65536 / n2;
}
public List<String> split2(String str) {
return IntStream.range(0, str.length() - 1)
.mapToObj(i -> str.substring(i, i + 2))
.map(String::toUpperCase)
.filter(s -> Pattern.matches("[A-Z]+", s))
.collect(Collectors.toList());
}
public int intersectCount(List<String> s1, List<String> s2) {
List<String> ss2 = new LinkedList<>(s2);
int count = 0;
for (String s : s1) {
if (ss2.remove(s)) count += 1;
}
return count;
}
public int unionCount(List<String> s1, List<String> s2) {
List<String> ss2 = new LinkedList<>(s2);
int count = 0;
for (String s : s1) {
ss2.remove(s);
count += 1;
}
return count + ss2.size();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment