Skip to content

Commit c9fe7d2

Browse files
authored
[20250812] BOJ / G5 / 회문 / 이강현
1 parent f9e1a17 commit c9fe7d2

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

lkhyun/202508/12 BOJ G5 회문.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
9+
int T = Integer.parseInt(br.readLine());
10+
11+
StringBuilder sb = new StringBuilder();
12+
for (int i = 0; i < T; i++) {
13+
String str = br.readLine();
14+
sb.append(checkPalindrome(str)).append('\n');
15+
}
16+
17+
bw.write(sb.toString());
18+
bw.close();
19+
}
20+
private static boolean isPalindrome(String str, int start, int end) {
21+
while (start < end) {
22+
if (str.charAt(start) != str.charAt(end)) {
23+
return false;
24+
}
25+
start++;
26+
end--;
27+
}
28+
return true;
29+
}
30+
31+
private static int checkPalindrome(String str) {
32+
int left = 0;
33+
int right = str.length() - 1;
34+
35+
while (left < right) {
36+
if (str.charAt(left) == str.charAt(right)) {
37+
left++;
38+
right--;
39+
} else {
40+
if (isPalindrome(str, left + 1, right) || isPalindrome(str, left, right - 1)) {
41+
return 1;
42+
} else {
43+
return 2;
44+
}
45+
}
46+
}
47+
return 0;
48+
}
49+
}
50+
```

0 commit comments

Comments
 (0)