描述
给定一个字符串,问是否能够通过添加一个字母将其变成“回文串”。 “回文串”是指正着和反着读都一样的字符串。如:”aa”,”bob”,”testset”是回文串,”alice”,”time”都不是回文串。
输入描述
一行一个有小写字母构成的字符串,字符串长度不超过10。
输出描述
如果输入字符串可以通过添加一个字符,则输出”YES”,否则输出”NO”。
示例
输 入:
coco
返回值:
Yes
package main;
import java.util.Scanner;
/**
* 回文
*
*
*/
public class HuiWen {
public static boolean isPalindrome(String str) {
int left = 0;
int right = str.length() - 1;
int flg = 0;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
flg++;
if(str.charAt(left+1) == str.charAt(right)) {
left++;
}else {
right--;
}
} else {
left++;
right--;
}
if (flg > 1) {
return false;
}
}
return true;
}
public static void main(String[] args) {
String str = "coco";
// Scanner scanner = new Scanner(System.in);
Scanner scanner = new Scanner(str);
Boolean bo;
bo = isPalindrome(str);
if (bo) {
System.out.println("Yes");
} else {
System.out.println("NO");
}
}
}