Created
July 25, 2023 07:31
-
-
Save youngvctr/bb67c41a887394b43655b01b0a2b15d8 to your computer and use it in GitHub Desktop.
Programmers | 2020 KAKAO BLIND RECRUITMENT > 문자열 압축
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int solution(String s) { | |
int answer = s.length(); | |
StringBuffer sb = new StringBuffer(); | |
for(int i=1; i<=s.length()/2; i++){ | |
int count = 1; | |
String repeat = s.substring(0, i); | |
for(int j=i; j<=s.length(); j += i){ | |
String curStr = j+i > s.length() ? s.substring(j, s.length()) : s.substring(j, j+i); | |
if(curStr.equals(repeat)){ | |
count++; | |
} else { | |
String temp = count > 1 ? String.valueOf(count) : ""; | |
sb.append(temp).append(repeat); | |
repeat = curStr; | |
count = 1; | |
} | |
} | |
sb.append(repeat); | |
answer = Math.min(answer, sb.length()); | |
sb.setLength(0); | |
} | |
return answer; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment