125. Valid Palindrome
How to Check if a String is a Palindrome in JavaScript: A Step-by-Step Guide
Palindromes are fascinating! Whether it's the word "racecar" or the phrase "A man, a plan, a canal, Panama," there's something intriguing about a sequence that reads the same backward and forward. If you’re looking to check if a string is a palindrome using JavaScript, you’re in the right place. Let's break down a simple approach to do just that.
What is a Palindrome?
A palindrome is a string that reads the same backward as forward. When checking for palindromes, we often ignore non-alphanumeric characters (like punctuation and spaces) and case differences. This means "A man, a plan, a canal: Panama" is considered a palindrome.
Problem Statement
Given a string s
, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Return true
if it is a palindrome, or false
otherwise.
Step-by-Step Approach
Clean the String: Remove all non-alphanumeric characters and convert the string to lowercase.
Initialize Pointers: Use two pointers to check characters from both ends of the cleaned string.
Compare Characters: Move the pointers towards the center, comparing characters. If any pair of characters don’t match, the string isn’t a palindrome.
Return Result: If all characters match, return
true
; otherwise, returnfalse
.
Code Implementation
Here's how you can implement this in JavaScript:
var isPalindrome = function(s) {
s = s.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
let left = 0;
let right = s.length - 1;
while(left < right) {
if(s[right] !== s[left]) {
return false;
}
right--;
left++;
}
return true;
};
Explanation
Clean the String:
s.replace(/[^a-zA-Z0-9]/g, "").toLowerCase()
removes all non-alphanumeric characters and converts the string to lowercase.
Initialize Pointers:
let left = 0;
initializes the left pointer at the start of the string.let right = s.length - 1;
initializes the right pointer at the end of the string.
Compare Characters:
The
while
loop continues as long asleft
is less thanright
.Inside the loop, if the characters at the
left
andright
pointers don’t match, returnfalse
.Otherwise, move
right
leftward andleft
rightward to continue comparing the next set of characters.
Return Result:
If the loop completes without finding any mismatched characters, return
true
.
Complexity Analysis
Time Complexity:
O(n)
, wheren
is the length of the strings
. This is because we iterate through the string once to clean it and once more to check for palindrome properties.Space Complexity:
O(1)
, since we only use a few extra variables regardless of the input size.
Conclusion
This solution provides an efficient and easy-to-understand method for checking if a string is a palindrome in JavaScript. By cleaning the string and using a two-pointer technique, we can ensure that our solution handles various edge cases and performs well.
Whether you’re preparing for coding interviews, solving problems on LeetCode, or simply exploring string algorithms, understanding how to implement and optimize palindrome checks is a valuable skill.
Happy coding! š
leetcode solution : https://leetcode.com/problems/valid-palindrome/solutions/6140528/twopinter-solution-in-o-n/
Comments
Post a Comment