Simple Clean Java Solution


  • 0
    M
        public boolean validPalindrome(String s) {
            if (s == null) return false;
            if (s.length() == 0) return true;
            int lo = 0, hi = s.length() - 1;
            while (lo < hi) {
                if (s.charAt(lo) == s.charAt(hi)) {
                    ++lo;
                    --hi;
                } else {
                    return valid(s, lo + 1, hi) || valid(s, lo, hi - 1);			
                }
            }
            return true;
        }
        private boolean valid(String s, int lo, int hi) {
            while (lo < hi) {
                if (s.charAt(lo) != s.charAt(hi)) {
                    return false;
                }
                ++lo;
                --hi;
            }
            return true;
        }
    

Log in to reply
 

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