题目描述
给定一个字符串,问是否能通过添加一个字母将其变为回文串。
输入描述:
一行一个由小写字母构成的字符串,字符串长度小于等于10。输出描述:
输出答案(YES\NO).输入
coco输出
YES
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
// BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
// String str = bf.readLine();
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();
char[] ch = str.toCharArray();
int l = 0;
int r = str.length()-1;
//boolean com = compare(ch,l,r);
while (l<r){
if(ch[l]!=ch[r]){
if(compare(ch,l+1,r)||compare(ch,l,r-1)){
System.out.println("YES");
break;
}else {
System.out.println("NO");
break;
}
}
l++;
r--;
}
}
}
private static boolean compare(char[] ch, int l, int r) {
while(l<r){
if(ch[l]!=ch[r]){
return false;
}
l++;
r--;
}
return true;
}
}