728x90
import java.util.*;
class Solution {
int answer = 0;
public int solution(String s) {
answer = s.length();
for(int splitCount = 1 ; splitCount < s.length() ; splitCount++){
String[] strArray = s.split("(?<=\\G.{"+splitCount+"})");
compress(strArray);
}
return answer;
}
public void compress(String[] array){
// System.out.println(Arrays.toString(array));
int count = 1;
for(int i = 0 ; i < array.length-1;i++){
if(array[i].equals(array[i+1])){
if(count > 1){
array[i-1] = "";
}
count++;
array[i+1] = array[i];
array[i] = String.valueOf(count);
// System.out.println(Arrays.toString(array));
}else{
count = 1;
}
}//end of for loop
calculate(array);
}
public void calculate(String[] array){
String str = "";
for(int i = 0 ; i < array.length ; i++){
str = str + array[i];
}
// System.out.println(str);
if(answer > str.length()){
answer = str.length();
}
}
}
728x90
'Algorithm > Programmers' 카테고리의 다른 글
Programmers - 프린터 (0) | 2020.05.15 |
---|---|
Programmers - [1차] 프렌즈4블록 (0) | 2020.05.08 |
Programmers - 체육복 (0) | 2020.04.11 |
Programmers - 크레인 인형뽑기 게임 (0) | 2020.04.10 |
Programmers - 이분탐색.예산 (0) | 2020.04.07 |
댓글