11class Solution {
22 public int solution (String s ) {
33 int answer = s .length ();
4-
5- int maxLength = s .length ()/2 ;
6-
7- for (int i =1 ; i <=maxLength ; i ++) {
8-
9- String currentAnswer = "" ;
10- int count = 1 ;
11- String currentString = s .substring (0 , i );
12- for (int j = i ; j < s .length (); j +=i ) {
13- String currentString2 = s .substring (j , Math .min (j + i , s .length ()));
14- if (currentString .equals (currentString2 )) {
15- count +=1 ;
16- }
17- else {
18- if (count !=1 ) {
19- currentAnswer += count ;
20- }
21- currentAnswer += currentString ;
22- currentString = currentString2 ;
23- count =1 ;
24- }
25- }
4+ for (int i = 1 ; i <= s .length ()/2 ; i ++) { //i는 자를 갯수
5+ // 맨 앞에부터 시작해서 크기만큼 잘라서 그걸로 이어가기 해야함.
266
27- if (count != 1 ) currentAnswer += count ;
28- currentAnswer += currentString ;
29- if (currentAnswer .length () < answer ) {
30- answer = currentAnswer .length ();
7+ String sub = s .substring (0 , i );
8+ StringBuilder sb = new StringBuilder ();
9+ int count = 1 ;
10+ for (int j =i ; j <s .length (); j +=i ) {
11+ int endIdx = Math .min (j + i , s .length ());
12+ String currentSub = s .substring (j , endIdx );
13+
14+ if (sub .equals (currentSub )) {
15+ count ++;
16+ } else {
17+ if (count > 1 ) {
18+ sb .append (count );
19+ }
20+ sb .append (sub );
21+ count = 1 ;
22+ sub = currentSub ;
23+ }
3124 }
25+ if (count >1 ) sb .append (count );
26+ sb .append (sub );
27+ answer = Math .min (sb .length (), answer );
3228 }
3329
3430 return answer ;
3531 }
36- }
37-
38- // 1. 1개 자르는 것부터 n/2까지 진행
39- // 2.
32+ }
0 commit comments