Skip to content

Commit 4f67f42

Browse files
authored
팰린드롬 만들기 (#61)
1 parent 2063044 commit 4f67f42

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
static String str;
6+
static int min = Integer.MAX_VALUE;
7+
8+
public static void main(String[] args) throws IOException {
9+
Scanner sc = new Scanner(System.in);
10+
str = sc.next();
11+
solve();
12+
13+
}
14+
15+
public static void solve() {
16+
for (int i = 0; i < str.length(); i++) {
17+
String left = str.substring(0, i);
18+
String right = str.substring(i);
19+
action(left, right);
20+
}
21+
System.out.println(min);
22+
}
23+
24+
public static void action(String left, String right) {
25+
// 일단 right가 팰린드롬이어야 한다.
26+
if (isPalindrom(right)) {
27+
min = Math.min(min, left.length() * 2 + right.length());
28+
}
29+
}
30+
31+
public static boolean isPalindrom(String right) {
32+
int mid = right.length() / 2;
33+
for (int i = 0; i < mid; i++) {
34+
if (right.charAt(i) != right.charAt(right.length() - 1 - i)) {
35+
return false;
36+
}
37+
}
38+
return true;
39+
}
40+
41+
}

0 commit comments

Comments
 (0)