Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- back-end
- Java8
- 자료구조
- lambda
- 모던자바
- OS
- LEVEL2
- 코틀린
- baekjoon
- 스프링
- DP
- kotlin
- DFS
- backtracking
- TDD
- 백트래킹
- 프로그래머스
- 네트워크
- algorithm
- 자바
- BFS
- 알고리즘
- java
- Spring
- 백준
- programmers
- 운영체제
- 그래프
- 프로젝트
- Brute-force
Archives
- Today
- Total
요깨비's LAB
[프로그래머스, JAVA] 뉴스 클러스터링 본문
import java.util.*
class Solution {
public int solution(String str1, String str2) {
int answer = 0;
str1 = str1.toUpperCase();
str2 = str2.toUpperCase();
Map<String, Integer> str1Map = new HashMap<>();
Map<String, Integer> str2Map = new HashMap<>();
setMap(str1Map, str1);
setMap(str2Map, str2);
double intersaction = getIntersection(str1Map, str2Map);
double union = getUnion(str1Map, str2Map);
double value = 1;
if((int)union == 0 && (int)intersaction == 0) {
value = 1;
}else {
value = intersaction / union;
}
answer = (int)(value * 65536);
return answer;
}
public void setMap(Map<String, Integer> map, String str) {
int str1Len = str.length();
String[] condition = {"~","!","@","#","$"};
for (int i = 0; i < str1Len - 1; i++) {
String element = str.substring(i, i + 2);
if(!element.matches("[A-Z]*")) {
continue;
}
if (map.containsKey(element)) {
int count = map.get(element);
map.replace(element, count + 1);
} else {
map.put(element, 1);
}
}
}
public int getIntersection(Map<String, Integer> str1Map, Map<String, Integer> str2Map) {
Set<Integer> intersactionSet = new HashSet<>();
Iterator<String> iterator = str1Map.keySet().iterator();
int intersaction = 0;
while(iterator.hasNext()) {
String key= iterator.next();
if(str2Map.containsKey(key)) {
if(str1Map.get(key) > str2Map.get(key)) {
intersaction += str2Map.get(key);
}else {
intersaction += str1Map.get(key);
}
}
}
return intersaction;
}
public int getUnion(Map<String, Integer> str1Map, Map<String, Integer> str2Map) {
Set<String> unionSet = new HashSet<>();
int union = 0;
Iterator<String> str1Iterator = str1Map.keySet().iterator();
while(str1Iterator.hasNext()) {
String key= str1Iterator.next();
if(str2Map.containsKey(key)) {
if(str1Map.get(key) < str2Map.get(key)) {
union += str2Map.get(key);
}else {
union += str1Map.get(key);
}
}
}
str1Iterator = str1Map.keySet().iterator();
while(str1Iterator.hasNext()) {
String key = str1Iterator.next();
if(!str2Map.containsKey(key)) {
union += str1Map.get(key);
}
}
Iterator<String> str2Iterator = str2Map.keySet().iterator();
while(str2Iterator.hasNext()) {
String key = str2Iterator.next();
if(!str1Map.containsKey(key)) {
union += str2Map.get(key);
}
}
return union;
}
}
'알고리즘(Java) > 프로그래머스' 카테고리의 다른 글
[프로그래머스, Java] 메뉴 리뉴얼 (0) | 2021.08.17 |
---|---|
[프로그래머스, JAVA] 행렬 테두리 회전하기 (0) | 2021.06.15 |
[프로그래머스, JAVA] 단체사진 찍기 (0) | 2021.06.05 |
[프로그래머스, JAVA] 저울 (0) | 2020.02.12 |
[프로그래머스, JAVA] 피보나치 수 (0) | 2019.12.23 |
Comments