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

  1. Clean the String: Remove all non-alphanumeric characters and convert the string to lowercase.

  2. Initialize Pointers: Use two pointers to check characters from both ends of the cleaned string.

  3. Compare Characters: Move the pointers towards the center, comparing characters. If any pair of characters don’t match, the string isn’t a palindrome.

  4. Return Result: If all characters match, return true; otherwise, return false.

Code Implementation

Here's how you can implement this in JavaScript:

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

  1. Clean the String:

    • s.replace(/[^a-zA-Z0-9]/g, "").toLowerCase() removes all non-alphanumeric characters and converts the string to lowercase.

  2. 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.

  3. Compare Characters:

    • The while loop continues as long as left is less than right.

    • Inside the loop, if the characters at the left and right pointers don’t match, return false.

    • Otherwise, move right leftward and left rightward to continue comparing the next set of characters.

  4. Return Result:

    • If the loop completes without finding any mismatched characters, return true.

Complexity Analysis

  • Time Complexity: O(n), where n is the length of the string s. 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

Popular posts from this blog

3Sum Closest: O(n²) Two-Pointer Magic šŸš€ leetcode: 16

Kadane's Algorithm: Maximum Subarray Problem - LeetCode(53) Solution

14. Longest Common Prefix