Search⌘ K
AI Features

Valid Palindrome

Explore how to verify if a string is a valid palindrome by converting letters to lowercase and ignoring non-alphanumeric characters. Learn to apply the two pointers technique efficiently to solve this problem, helping you strengthen your coding interview skills in Go.

Statement

Given a string, s, return TRUE if it is a palindrome; otherwise, return FALSE.

A phrase is considered a palindrome if it reads the same backward as forward after converting all uppercase letters to lowercase and removing any characters that are not letters or numbers. Only alphanumeric characters (letters and digits) are taken into account.

Constraints:

  • 11 \leq s.length 3000\leq 3000

  • s consists only of printable ASCII characters.

Examples

canvasAnimation-image
1 / 7

Understand the problem

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

Valid Palindrome

1.

What is the output for the following input?

s = “A man, a plan, a canal: Panama”

A.

TRUE

B.

FALSE


1 / 5

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.

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.

Need a nudge?

Explore these hints—each one is designed to guide you a step closer to the solution.

Go
usercode > Solution.go
package main
import (
"unicode"
)
func isPalindrome(s string) bool {
left, right := 0, len(s)-1
for left < right {
for left < right && !unicode.IsLetter(rune(s[left])) && !unicode.IsDigit(rune(s[left])) {
left++
}
for left < right && !unicode.IsLetter(rune(s[right])) && !unicode.IsDigit(rune(s[right])) {
right--
}
if unicode.ToLower(rune(s[left])) != unicode.ToLower(rune(s[right])) {
// write your code here
}
left++;
right--;
}
return true
}
Valid Palindrome