> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Valid Palindrome II Python Solution with Tests

> Tested Python solution for LeetCode 680 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 680, Easy. Topics: Two Pointers, String, Greedy. [View on LeetCode](https://leetcode.com/problems/valid-palindrome-ii/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 680   # by problem number
lcpy gen -s valid_palindrome_ii   # by problem name
```

## Problem

Given a string `s`, return `true` if the `s` can be palindrome after deleting **at most one** character from it.

### Examples

```
Input: s = "aba"
Output: true
```

```
Input: s = "abca"
Output: true
Explanation: You could delete the character 'c'.
```

```
Input: s = "abc"
Output: false
```

### Constraints

* 1 \<= s.length \<= 10^5
* `s` consists of lowercase English letters.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/valid_palindrome_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/valid_palindrome_ii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def valid_palindrome(self, s: str) -> bool:
        def is_palindrome(left: int, right: int) -> bool:
            while left < right:
                if s[left] != s[right]:
                    return False
                left += 1
                right -= 1
            return True

        left, right = 0, len(s) - 1
        while left < right:
            if s[left] != s[right]:
                # Mismatch: try skipping either the left or the right character.
                return is_palindrome(left + 1, right) or is_palindrome(left, right - 1)
            left += 1
            right -= 1
        return True
```

## Complexity

| Time | Space |
| ---- | ----- |
| O(n) | O(1)  |

## Tags

[NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
