# 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.

## Thoughts

问给定字符串至多删去一个字符能否构成回文。只有一个字符不满足的话当遇到那个字符时跳过它就还是palindrome，跳过的方式有从左或右跳过。

## Code

```cpp
/*
 * @lc app=leetcode id=680 lang=cpp
 *
 * [680] Valid Palindrome II
 */

// @lc code=start
class Solution {
public:
    bool validPalindrome(string s) {
        const auto palind = [&](int l, int r) {
            while (l < r) {
                if (s[l] != s[r]) return false;
                else {
                    ++l;
                    --r;
                }
            }
            return true;
        };
        for (int l = 0, r = s.length() - 1; l < r; ++l, --r) {
            if (s[l] != s[r]) {
                return palind(l + 1, r) || palind(l, r - 1);
            }
        }
        return true;
    }
};
// @lc code=end


```

```java
class Solution {
    private boolean isPalin(String s, int start, int end) {
        for (; start < end; start++, end--) {
            if (s.charAt(start) != s.charAt(end)) {
                return false;
            }
        }
        return true;
    }

    public boolean validPalindrome(String s) {
        for (int l = 0, r = s.length() - 1; l < r; l++, r--) {
            if (s.charAt(l) == s.charAt(r)) {
                continue;
            } else {
                return isPalin(s, l + 1, r) || isPalin(s, l, r - 1);
            }
        }

        return true;
    }
}
```

## Analysis

时间O(N).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://hao-fu-1.gitbook.io/oj/multiple-pointers/valid-palindrome/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.
