[AC]clean java solution O(n) time O(1) space


  • 0
    Y
    
    class Solution {
        public boolean validPalindrome(String s) {
            for (int i = 0, j = s.length() - 1; i <= j; i++, j--) {
                if (s.charAt(i) != s.charAt(j)) return helper(s, i, j - 1) || helper(s, i + 1, j);
            }
            return true;
        }
        public boolean helper(String s, int start, int end) {
            for (int i = start, j = end; i <= j; i++, j--) {
                if (s.charAt(i) != s.charAt(j)) return false;
            }
            return true;
        }
    }
    

Log in to reply
 

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