java solution


  • 0
    2
    class Solution {
        public boolean validPalindrome(String s) {
            if (isP(s)) {
                return true;
            }
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
                    return isP(s.substring(0, i) + s.substring(i + 1)) ||
                        isP(s.substring(0, s.length() - 1 - i) + s.substring(s.length() - i));
                }
            }
            return true;
        }
        
        private boolean isP(String s) {
            return s.substring(0, s.length() / 2)
                .equals(new StringBuilder(s).reverse().toString().substring(0, s.length() / 2));
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.