๐ ๋ฌธ์
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
๐ ํ์ด
- ๋ฌธ์ ์์ ์ ์ํ๋๋ก ๊ตฌํํ๋ฉด ๋๋ ๋ฌธ์ ์ ๋๋ค.
- ํด๋น ๋ฌธ์ ๋ฅผ ๊ตฌํํ ๋ ํต์ฌ์ ์ธ ๋ถ๋ถ์ map์ ์ฌ์ฉํ์ฌ ๊ต์งํฉ๊ณผ ํฉ์งํฉ์ ๊ตฌํ๋ ๋ถ๋ถ์ ๋๋ค.
- ๋ฌธ์ ์์ min ๊ณผ max๋ฅผ ์ฌ์ฉํ์ฌ ๊ต์งํฉ๊ณผ ํฉ์งํฉ์ ๊ตฌํ๋ ํ์ด๋ฅผ ์๋ ค์ฃผ์์ผ๋ฏ๋ก ๊ทธ๋๋ก ๊ตฌํํ๋ฉด ๋ฉ๋๋ค.
- ํ์ด์ ์์๋
- ๊ต์งํฉ๊ณผ ํฉ์งํฉ์ ๊ตฌํ ์ ์๋๋ก ์ฃผ์ด์ง String์ ํตํด Map์ ๋ง๋ญ๋๋ค.
- ์์ชฝ map์ ๋์ผํ key๊ฐ์ด ์๋์ง ํ์ธํ๋ ๋ฐฉ์์ผ๋ก ๊ต์งํฉ๊ณผ ํฉ์งํฉ์ ๊ตฌํฉ๋๋ค.
- (๊ต์งํฉ / ํฉ์งํฉ) * 65536 ์ ์ด์ฉํ์ฌ ์ต์ข ๊ฐ์ ๋ฆฌํดํฉ๋๋ค.
๐ ์ฝ๋
import java.util.*;
class Solution {
public int solution(String str1, String str2) {
int answer = 0;
double union = 0; //ํฉ์งํฉ
double inter = 0; //๊ต์งํฉ
HashMap<String, Integer> map1 = new HashMap<>();
HashMap<String, Integer> map2 = new HashMap<>();
str1 = str1.toUpperCase();
str2 = str2.toUpperCase();
//๋๊ฐ์ ์ฐ๊ฒฐ๋ ๋ฌธ์๋ฅผ key๊ฐ์ผ๋ก ๋ช ๊ฐ์ธ์ง ์
ํ์ฌ map์ ๋ฃ์ด์ค๋ค.
for(int i =0; i< str1.length() -1; i++){
String s = str1.substring(i, i+2).replaceAll("[^A-Z]", "");
if(s.length() < 2) continue;
map1.put(s, map1.getOrDefault(s, 0) + 1);
}
for(int i =0; i< str2.length() -1; i++){
String s = str2.substring(i, i+2).replaceAll("[^A-Z]", "");
if(s.length() < 2) continue;
map2.put(s, map2.getOrDefault(s, 0) + 1);
}
if(map1.size() == 0 && map2.size() == 0) return 65536;
//๋ฌธ์ ์์ ์ ์ํ ํ์ด๋๋ก ํฉ์งํฉ๊ณผ ๊ต์งํฉ์ ๊ตฌํ๋ค.
for(String key : map1.keySet()){
if(map2.containsKey(key)){
inter += Math.min(map1.get(key), map2.get(key));
union += Math.max(map1.get(key), map2.get(key));
map2.remove(key);
} else {
union += map1.get(key);
}
}
for(String key : map2.keySet()){
union += map2.get(key);
}
return (int) Math.floor((inter/union) * 65536);
}
}