Um momento
Leetcode / 680. Valid Palindrome II

Pick a programming language:

Here is the source code for the solution to this problem.

class Solution {
    // See if substring [start, end] is a palindrome.
    private boolean isPalindrome(String s, int start, int end) {
        int i = start;
        int j = end;
        while (i < j) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }

    public boolean validPalindrome(String s) {
        int i = 0;
        int j = s.length() - 1;
        while (i < j) {
            if (s.charAt(i) != s.charAt(j)) {
                return isPalindrome(s, i + 1, j) || isPalindrome(s, i, j - 1);
            }
            i++;
            j--;
        }
        return true;
    }
}
Gostou da aula? 😆👍
Apoie nosso trabalho com uma doação: