https://school.programmers.co.kr/learn/courses/30/lessons/17684
인덱스 잘 체크해서 구현
import java.util.*;
import java.io.*;
class Solution {
static Map<String, Integer> dict;
static {
dict = new HashMap<>();
for(int i = 0; i < 26; i++){
dict.put(""+(char)('A'+i), i+1);
}
}
public int[] solution(String msg) {
List<Integer> ans = new ArrayList<>();
int i = 0;
while(i < msg.length()){
int j = i+1;
while(j <= msg.length()) {
String maxLenStr = msg.substring(i, j);
if(!dict.keySet().contains(maxLenStr)) {
dict.put(maxLenStr, dict.size()+1);
break;
}
j+=1;
}
ans.add(dict.get(msg.substring(i,j-1)));
i = j-1;
}
return ans.stream().mapToInt(x->x).toArray();
}
}
[프로그래머스] 가장 큰 수 (0) | 2025.02.04 |
---|---|
[프로그래머스] 땅따먹기 (0) | 2025.02.04 |
[백준] 14426: 접두사 (0) | 2025.02.03 |
[프로그래머스] 방문길이 (0) | 2025.02.03 |
[프로그래머스] 롤케이크 (0) | 2025.02.03 |