File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments