-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path메뉴리뉴얼.java
69 lines (50 loc) · 1.75 KB
/
메뉴리뉴얼.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package 프로그래머스.level2;
import java.util.*;
public class 메뉴리뉴얼 {
static Map<String, Integer> map;
static int max;
public static void main(String[] args) {
System.out.println(Arrays.toString(new 메뉴리뉴얼().solution(new String[]{"ABCFG", "AC", "CDE", "ACDE", "BCFG", "ACDEH"}, new int[]{2, 3, 4})));
}
private static String sortStr(String origin) {
char[] result = origin.toCharArray();
Arrays.sort(result);
return new String(result);
}
public static void combination(int cnt, int start, int end, String order, String oneCase) {
if (cnt == end) {
String sorted = sortStr(oneCase);
if (map.get(sorted) == null) {
map.put(sorted, 1);
} else {
int next = map.get(sorted) + 1;
map.replace(sorted, next);
max = Math.max(max, next);
}
return;
}
for (int i = start; i < order.length(); i++) {
combination(cnt + 1, i + 1, end, order, oneCase + order.charAt(i));
}
}
public String[] solution(String[] orders, int[] course) {
List<String> result = new ArrayList<>();
for (int menuCount : course) {
map = new HashMap<>();
max = 0;
for (String order : orders) {
combination(0, 0, menuCount, order, "");
}
if (max < 2) {
continue;
}
for (String key : map.keySet()) {
if (map.get(key) == max) {
result.add(key);
}
}
}
Collections.sort(result);
return result.toArray(new String[result.size()]);
}
}