File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ public static void main (String [] args ) throws Exception {
7+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
8+ StringTokenizer st;
9+
10+ int T = Integer . parseInt(br. readLine());
11+
12+ StringBuilder sb = new StringBuilder ();
13+ while (T -- > 0 ) {
14+ String s = br. readLine();
15+ sb. append(palindrom(s, 0 , s. length() - 1 , 0 )). append(" \n " );
16+ }
17+ System . out. print(sb);
18+ }
19+
20+ static int palindrom (String s , int left , int right , int deleted ) {
21+ if (deleted >= 2 ) return 2 ;
22+
23+ while (left < right) {
24+ if (s. charAt(left) == s. charAt(right)) {
25+ left++ ;
26+ right-- ;
27+ } else {
28+ int resLeft = palindrom(s, left + 1 , right, deleted + 1 );
29+ int resRight = palindrom(s, left, right - 1 , deleted + 1 );
30+
31+ return Math . min(resLeft, resRight);
32+ }
33+ }
34+
35+ return deleted;
36+ }
37+ }
38+ ```
You can’t perform that action at this time.
0 commit comments