# 680.Valid-Palindrome-II

## 680. Valid Palindrome II

## 题目地址

<https://leetcode.com/problems/valid-palindrome-ii/>

## 题目描述

```
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.

Example 1:
Input: "aba"
Output: True
Example 2:
Input: "abca"
Output: True
Explanation: You could delete the character 'c'.
Note:
The string will only contain lowercase characters a-z. The maximum length of the string is 50000.
```

## 代码

### Approach #1 Greedy

```java
class Solution {
  public boolean validPalindrome(String s) {
    int n = s.length();
        for (int i = 0; i < n / 2; i++) {
      if (s.charAt(i) != s.charAt(n - 1 - i)) {
        int j = s.length() - 1 - i;
        return (isPalindromeRange(s, i + 1, j)
               || isPalindromeRange(s, i, j - 1));
      }
    }
  }

  private boolean isPalindromeRange(String s, int i, int j) {
    for (int k = i; k <= i + (j - i) / 2; k++) {
      if (s.charAt(k) != s.charAt(j - k + i))    {
        return false;
      }
    }
    return true;
  }

}
```

### Approach #2

```java
class Solution {
  public boolean validPlindrome(String s) {
    for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
      if (s.charAt(i) != s.charAt(j)) {
        int i1 = i, j1 = j - 1;
        int i2 = i + 1, j2 = j;
        while (i1 < j1 && s.charAt(i1) == s.charAt(j1)) {
          i1++;
          j1--;
        }
        while (i2 < j2 && s.charAt(i2) == s.charAt(j2)) {
          i2++;
          j2--;
        }

        return i1 >= j1 || i2 >= j2;
      }
    }
    return true;
  }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wentao-shao.gitbook.io/leetcode/two-pointers/680.valid-palindrome-ii.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
