Skip to content

Commit e40bcfb

Browse files
committed
[20251219] BOJ / G5 / 회문 / 김민진
1 parent 42aff2e commit e40bcfb

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
```java
2+
import java.io.*;
3+
4+
public class BJ_17609_회문 {
5+
6+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
private static final StringBuilder sb = new StringBuilder();
9+
10+
private static int N;
11+
private static String input;
12+
13+
public static void main(String[] args) throws IOException {
14+
N = Integer.parseInt(br.readLine());
15+
16+
while (N-- > 0) {
17+
input = br.readLine();
18+
sb.append(sol(0, input.length() - 1, 0)).append("\n");
19+
}
20+
bw.write(sb.toString());
21+
bw.flush();
22+
br.close();
23+
bw.close();
24+
}
25+
26+
private static int sol(int left, int right, int cnt) {
27+
if (cnt >= 2) {
28+
return 2;
29+
}
30+
31+
while (left < right) {
32+
if (input.charAt(left) == input.charAt(right)) {
33+
left++;
34+
right--;
35+
} else {
36+
return Math.min(sol(left + 1, right, cnt + 1), sol(left, right - 1, cnt + 1));
37+
}
38+
}
39+
return cnt;
40+
}
41+
42+
}
43+
```

0 commit comments

Comments
 (0)