-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.java
48 lines (38 loc) Β· 1.56 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package _2020_KAKAO_BLIND_RECRUITMENT.P1;
public class Main {
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(sol.solution("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
System.out.println(sol.solution("aabbaccc"));
System.out.println(sol.solution("ababcdcdababcdcd"));
System.out.println(sol.solution("abcabcdede"));
System.out.println(sol.solution("abcabcabcabcdededededede"));
System.out.println(sol.solution("xababcdcdababcdcd"));
}
}
class Solution {
static String target, compare;
public int solution(String s) {
int answer = s.length();
for (int cnt = 1; cnt <= s.length() / 2; cnt++) { // cnt λ¨μλ‘ λ¬Έμμ΄ μλ₯΄κΈ°
int case_ = 0;
target = s.substring(0, cnt);
int count = 1;
for (int i = cnt; i < s.length(); i += cnt) {
compare = (i + cnt <= s.length()) ? s.substring(i, i + cnt) : s.substring(i);
if (target.equals(compare)) {
count ++;
} else {
if (count == 1) case_ += cnt;
else case_ += cnt + (int)(Math.log10(count)+1);
count = 1;
target = compare;
}
}
if (count == 1) case_ += compare.length();
else case_ += cnt + (int)(Math.log10(count)+1);
answer = Math.min(answer, case_);
}
return answer;
}
}