-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPG_160586.java
More file actions
41 lines (34 loc) · 1.21 KB
/
PG_160586.java
File metadata and controls
41 lines (34 loc) · 1.21 KB
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
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
// https://school.programmers.co.kr/learn/courses/30/lessons/160586
// 프로그래머스 키패드 누르기
class Solution {
public int[] solution(String[] keymap, String[] targets) {
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < keymap.length; i++) {
for (int j = 0; j < keymap[i].length(); j++) {
//최소 횟수 버튼값 추가
Character button = keymap[i].charAt(j);
int count = j + 1;
map.merge(button, count, (v1, v2) -> Math.min(v1, v2));
}
}
// System.out.println(map);
return Arrays.stream(targets)
.mapToInt(target -> targetToCount(map, target))
.toArray();
}
private int targetToCount(Map<Character, Integer> map, String target) {
int sum = 0;
for (int i = 0; i < target.length(); i++) {
char ch = target.charAt(i);
if (!map.containsKey(ch)) {
return -1;
} else {
sum += map.get(ch);
}
}
return sum;
}
}