Skip to content

Commit 600d446

Browse files
committed
[level 2] Title: 문자열 압축, Time: 4.04 ms, Memory: 97.3 MB -BaekjoonHub
1 parent 489da11 commit 600d446

File tree

2 files changed

+25
-32
lines changed

2 files changed

+25
-32
lines changed

프로그래머스/2/60057. 문자열 압축/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### 성능 요약
66

7-
메모리: 82.2 MB, 시간: 20.35 ms
7+
메모리: 97.3 MB, 시간: 4.04 ms
88

99
### 구분
1010

@@ -16,7 +16,7 @@
1616

1717
### 제출 일자
1818

19-
2025년 10월 07일 13:15:06
19+
2025년 11월 10일 19:51:08
2020

2121
### 문제 설명
2222

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,32 @@
11
class 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

Comments
 (0)