🖇️ 문제 링크
⌨️ 코드
import java.util.*;
class Solution {
Map<String, Integer>[] mArr;
int[] max;
char[] pick;
public String[] solution(String[] orders, int[] course) {
List<String> ret = new ArrayList<>();
mArr = new Map[11];
max = new int[11];
for(int i = 2; i <= 10; i++)
mArr[i] = new HashMap<>();
for(String order : orders) {
pick = new char[order.length()];
char[] sortOrder = order.toCharArray();
Arrays.sort(sortOrder);
comb(0, 0, sortOrder);
}
for(int c : course)
for(Map.Entry<String, Integer> e : mArr[c].entrySet()) {
if(max[c] >= 2 && e.getValue() == max[c])
ret.add(e.getKey());
}
ret.sort(String::compareTo);
return ret.toArray(new String[0]);
}
public void comb(int idx, int cnt, char[] order) {
if(cnt > order.length) return;
if(cnt >= 2) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < cnt; i++)
sb.append(pick[i]);
int value = mArr[cnt].getOrDefault(sb.toString(), 0);
mArr[cnt].put(sb.toString(), value + 1);
max[cnt] = Math.max(max[cnt], value + 1);
}
for(int i = idx; i < order.length; i++) {
pick[cnt] = order[i];
comb(i + 1, cnt + 1, order);
}
}
}
Uploaded by Notion2Tistory v1.1.0