Straight Forward isPalindrome() Modification


  • 0
    M
    class Solution {
        public boolean validPalindrome(String s) {
            return isValid(s, false);
        }
        
        public boolean isValid(String s, boolean used) {
            int i = 0;
            int j = s.length() - 1;
            while (i < j) {
                if (s.charAt(i) == s.charAt(j)) {
                    i += 1;
                    j -= 1;
                } else {
                    if (used) return false;
                    return isValid(s.substring(i, j), true) || isValid(s.substring(i + 1, j + 1), true);
                }
            }
            return true;
        }
    }

Log in to reply
 

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