Search⌘ K
AI Features

Valid Palindrome II

Explore how to determine if a string can become a valid palindrome by removing at most one character using two pointers. Understand the problem constraints and implement an optimal O(n) time and O(1) space solution in Go. This lesson guides you in applying the two-pointer pattern for efficient string traversal.

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 main.go 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.

Go
usercode > main.go
package main
func isPalindrome(str string) bool {
// Replace this placeholder return statement with your code
return false
}
Valid Palindrome II