原题链接:
在做完第一道回文字符串校验题目之后这第二道题目就很简单了:/** * Created by clearbug on 2018/2/26. */public class Solution { public static void main(String[] args) { Solution s = new Solution(); System.out.println(s.validPalindrome("abc")); } public boolean validPalindrome(String s) { if (s == null) { return false; } if (s.length() < 2) { return true; } return validPalindromeHelper(s.toCharArray(), 0, s.length() - 1, 1); } private boolean validPalindromeHelper(char[] chars, int start, int end, int maxDeleteTimes) { while (start < end) { if (chars[start] != chars[end]) { if (maxDeleteTimes > 0) { return validPalindromeHelper(chars, start + 1, end, maxDeleteTimes - 1) || validPalindromeHelper(chars, start, end - 1, maxDeleteTimes - 1); } else { return false; } } start++; end--; } return true; }}