Valid Palindrome
Try to solve the Valid Palindrome problem.
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:
s.length
s
consists only of printable ASCII characters.
Examples
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
What is the output for the following input?
s = “A man, a plan, a canal: Panama”
TRUE
FALSE
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.
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.
import java.util.*;public class Solution{public static boolean isPalindrome(String s) {int left = 0, right = s.length() - 1;while (left < right) {while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {left++;}while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {right--;}if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {// write your code here}left++;right--;}return true;}}