/**
* Author:SunLala
* Date: 2022/5/23
* 功能描述:()
*/
public class Test22 {
public static void main(String[] args) {
// System.out.println(palindrome("nowwon"));
// System.out.println(palindrome("nowewon"));
// System.out.println(palindrome("noweawon"));
// System.out.println(palindrome("noowwwn"));
System.out.println(validPalindrome("bdfsfdb"));
}
public static boolean validPalindrome (String s) {
int low = 0;
int high = s.length()-1;
while(low < high){
char c1 = s.charAt(low);
char c2 = s.charAt(high);
if(c1 == c2){
low = low+1;
high = high-1;
}else{
return validPalindrome(s,low+1,high) || validPalindrome(s,low,high-1);
}
}
return true;
}
private static boolean validPalindrome(String str, int low, int high) {
for (int i = low, j = high; i < j; ++i, --j) {
char c1 = str.charAt(i), c2 = str.charAt(j);
if (c1 != c2) {
return false;
}
}
return true;
}
}
给定一个非空字符串 s,最多删除一个字符。判断是否能成为回文字符串。
最新推荐文章于 2022-10-26 20:19:24 发布