Search⌘ K
AI Features

Valid Palindrome II

Explore how to verify if a given string can become a valid palindrome by removing at most one character. This lesson teaches you to apply the two-pointer technique to solve the problem efficiently, with linear time complexity and constant space. By practicing this approach, you'll enhance your skills in handling string and linear data structure problems relevant for coding interviews.

Statement

Write a function that takes a string as input and checks whether it can be a valid palindrome by removing at most one character from it.

Constraints:

  • 11 \leq string.length \leq 10510^5

  • The string only consists of English letters.

Examples

Understand the problem

Let's take a moment to make sure you've correctly understood the problem. The quiz below helps us to check that you're solving the correct problem:

Valid Palindrome II

1.

Can “RACEACAR” be a palindrome?

A.

Yes

B.

No


1 / 3

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding on how to solve this problem.

Note: As an additional challenge, we have intentionally hidden the solution to this puzzle.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5

Try it yourself

Implement your solution in the following coding playground. We have provided a useful code template in the other file that you may build on to solve this problem.

We have left the solution to this challenge as an exercise for you. The optimal solution to this problem runs in O(n) time and takes O(1) space. You may try to translate the logic of the solved puzzle into a coded solution.

Python
usercode > main.py
def is_palindrome(string):
# Replace this placeholder return statement with your code
return False
Valid Palindrome II