125. Valid Palindrome
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to determine if a given string is a palindrome. A string is a palindrome if it reads the same forwards and backwards. However, for this problem, we must first process the string by:
Solution
Valid Palindrome in Python
class Solution:
def isPalindrome(self, s: str) -> bool:
left, right = 0, len(s) - 1
while left < right:
# If the character at the left pointer is not alphanumeric, move the pointer to the right.
if not s[left].isalnum():
left += 1
continue
# If the character at the right pointer is not alphanumeric, move the pointer to the left.
if not s[right].isalnum():
right -= 1
continue
# Compare the two alphanumeric characters (case-insensitive).
if s[left].lower() != s[right].lower():
return False
# Move pointers inward for the next comparison.
left += 1
right -= 1
return True
Valid Palindrome in Java
class Solution {
public boolean isPalindrome(String s) {
int left = 0;
int right = s.length() - 1;
while (left < right) {
char leftChar = s.charAt(left);
char rightChar = s.charAt(right);
// If the left character is not alphanumeric, skip it
if (!Character.isLetterOrDigit(leftChar)) {
left++;
// If the right character is not alphanumeric, skip it
} else if (!Character.isLetterOrDigit(rightChar)) {
right--;
// Both are alphanumeric, so compare them
} else {
// Compare characters in lowercase
if (Character.toLowerCase(leftChar) != Character.toLowerCase(rightChar)) {
return false;
}
// Move pointers inward
left++;
right--;
}
}
return true;
}
}
Valid Palindrome in C++
class Solution {
public:
bool isPalindrome(string s) {
int left = 0, right = s.length() - 1;
while (left < right) {
// Move left pointer to the next alphanumeric character
// The check `left < right` is crucial to prevent pointers from crossing
while (left < right && !isalnum(s[left])) {
left++;
}
// Move right pointer to the previous alphanumeric character
while (left < right && !isalnum(s[right])) {
right--;
}
// Compare the characters in lowercase
if (tolower(s[left]) != tolower(s[right])) {
return false;
}
// Move pointers inward
left++;
right--;
}
return true;
}
};
Valid Palindrome in TypeScript
function isPalindrome(s: string): boolean {
let left = 0;
let right = s.length - 1;
// Regular expression to check for alphanumeric characters (letters and numbers).
const alphanumericRegex = /^[a-zA-Z0-9]$/;
while (left < right) {
// If the left character is not alphanumeric, skip it by moving the left pointer.
if (!alphanumericRegex.test(s[left])) {
left++;
continue;
}
// If the right character is not alphanumeric, skip it by moving the right pointer.
if (!alphanumericRegex.test(s[right])) {
right--;
continue;
}
// Both characters are alphanumeric, so we can compare them.
// Convert to lowercase for a case-insensitive comparison.
if (s[left].toLowerCase() !== s[right].toLowerCase()) {
return false;
}
// Move pointers inward to check the next pair of characters.
left++;
right--;
}
return true;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.