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
70
71
72
73
74
75
76
77
| import java.util.*;
class Solution {
public String solution(String m, String[] musicinfos) {
String answer = "";
Map<String, String> map = new HashMap<>();
Map<String, String> answerMap = new HashMap<>();
for(String musicInfo : musicinfos) {
String[] splitArray = musicInfo.split(",");
int playTime = getMusicTimeToInt(splitArray[1]) - getMusicTimeToInt(splitArray[0]);
String b = splitArray[3];
b = b.replaceAll("C#", "V");
b = b.replaceAll("D#", "W");
b = b.replaceAll("F#", "X");
b = b.replaceAll("G#", "Y");
b = b.replaceAll("A#", "X");
map.put(splitArray[2], getMusicTable(playTime, b));
}
for(String key : map.keySet()) {
if(isThatSong(m, map.get(key))) {
answerMap.put(key, map.get(key));
}
}
if(answerMap.size() == 0) return "(None)";
List<String> keyList = new ArrayList<>(answerMap.keySet());
Collections.sort(keyList, (o1, o2) -> answerMap.get(o2).length() - answerMap.get(o1).length());
return keyList.get(0);
}
private boolean isThatSong(String key, String song) {
key = key.replaceAll("C#", "V");
key = key.replaceAll("D#", "W");
key = key.replaceAll("F#", "X");
key = key.replaceAll("G#", "Y");
key = key.replaceAll("A#", "X");
loop: for(int i = 0; i < song.length() - key.length() + 1; i++) {
int kIndex = 0;
for(int j = i; j < i + key.length(); j++) {
if(key.charAt(kIndex) == song.charAt(j)) {
kIndex++;
} else {
continue loop;
}
}
return true;
}
return false;
}
private int getMusicTimeToInt(String time) {
return Integer.parseInt(time.split(":")[0]) * 60 + Integer.parseInt(time.split(":")[1]);
}
private String getMusicTable(int time, String table) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < time; i++) {
int index;
if(i >= table.length()) {
index = i % table.length();
} else {
index = i;
}
sb.append(table.charAt(index));
}
return sb.toString();
}
}
|