Skip to content

Commit d26a16f

Browse files
authored
Merge pull request #1315 from AlgorithmWithGod/lkhyun
[20251104] PGM / Lv2 / [3차] 압축 / 이강
2 parents 4253066 + 500c170 commit d26a16f

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
```java
2+
import java.util.*;
3+
4+
class Solution {
5+
public int[] solution(String msg) {
6+
List<Integer> answer = new ArrayList<>();
7+
Map<String, Integer> dictionary = new HashMap<>();
8+
for(int i = 1; i <= 26; i++){
9+
dictionary.put(String.valueOf((char)(64 + i)), i);
10+
}
11+
int dictionaryIdx = 27;
12+
13+
int cur = 0;
14+
while(cur < msg.length()){
15+
String w = String.valueOf(msg.charAt(cur));
16+
17+
while(cur + w.length() < msg.length() &&
18+
dictionary.containsKey(w + msg.charAt(cur + w.length()))){
19+
w = w + msg.charAt(cur + w.length());
20+
}
21+
answer.add(dictionary.get(w));
22+
23+
if(cur + w.length() < msg.length()){
24+
String wc = w + msg.charAt(cur + w.length());
25+
dictionary.put(wc, dictionaryIdx++);
26+
}
27+
28+
cur += w.length();
29+
}
30+
31+
return answer.stream().mapToInt(Integer::intValue).toArray();
32+
}
33+
}
34+
```

0 commit comments

Comments
 (0)