programmers.co.kr/learn/courses/30/lessons/60057
import java.util.Arrays;
// 압축했을 때, 가장 짧은 것의 길이는?
class Solution {
public int solution(String s) {
int[] compLengths = new int[s.length()];
for (int compSize = 1; compSize <= s.length(); compSize++){
String initialStr = s;
String totalCompStr = "";
String curCompStr = "";
int curCompCount = 1;
int startIndex = 0;
int endIndex = compSize;
while (endIndex <= s.length()){
String subS = s.substring(startIndex, endIndex);
initialStr = s.substring(endIndex);
if (!curCompStr.equals(subS)){
if (curCompCount > 1){
totalCompStr += curCompCount;
}
totalCompStr += curCompStr;
curCompStr = subS;
curCompCount = 1;
}else{
curCompCount += 1;
}
startIndex = endIndex;
endIndex += compSize;
}
if (curCompCount > 1){
totalCompStr += curCompCount;
}
totalCompStr += curCompStr;
totalCompStr += initialStr;
compLengths[compSize - 1] = totalCompStr.length();
}
Arrays.sort(compLengths);
return compLengths[0];
}
}
'Problem-solving > 프로그래머스' 카테고리의 다른 글
프로그래머스 - (2019 카카오 개발자 겨울 인턴십) 튜플 (Java) (0) | 2020.11.23 |
---|---|
프로그래머스 - (2020 KAKAO BLIND RECRUITMENT) 괄호 변환 (Java) (0) | 2020.11.23 |
프로그래머스 - (2018 KAKAO BLIND RECRUITMENT) [1차]다트 게임 (Java) (0) | 2020.11.22 |
프로그래머스 - (2019 KAKAO BLIND RECRUITMENT) 실패율 (Java) (0) | 2020.11.22 |
프로그래머스 - (2018 KAKAO BLIND RECRUITMENT) [1차]비밀지도 (Java) (0) | 2020.11.21 |
댓글