Valid Palindrome - JS Solution

JS
Editorial

Solution explanation

Solution explanation

Approach

We determine whether a string is a valid palindrome using a two-pointer technique that compares characters from both ends while ignoring non-alphanumeric symbols and case differences.

  • Core intuition:
    A palindrome mirrors itself β€” the first alphanumeric character must match the last alphanumeric character, the second alphanumeric must match the second-to-last alphanumeric, and so on. Because this relationship is symmetric around the center, it’s natural to start from both ends and move inward after each successful match. Each inward step confirms that one mirrored pair satisfies the palindrome property.

    If at any point the two compared characters don’t match (when compared case-insensitively), the symmetry is broken and we can stop immediately β€” the string is not a palindrome. Otherwise, the pointers keep moving inward until they cross, which happens naturally after the final pair matches, confirming that all mirrored pairs were valid.

    Note that if there is an odd number of alphanumeric characters, the pointers will meet at the middle. This meeting still results in a successful check where left == right at the center, and in the next iteration, they will naturally cross each other β€” marking the completion of the validation.
  • Pointer setup:
    Two pointers are used:
    • left starts at index 0 (the beginning of the string).
    • right starts at the last index (the end of the string).
    Both move toward the center as matching pairs are confirmed.
  • Skipping non-alphanumeric characters:
    Since only letters and digits matter for palindrome checking, any non-alphanumeric characters are skipped dynamically. The conditions while (left < right && !isAlphanumeric(s[left])) left++; and while (left < right && !isAlphanumeric(s[right])) right--; ensure that both pointers always land on meaningful (alphanumeric) characters before any comparison is made.

    Pointer safety and edge handling while skipping:
    The left < right part ensures that the pointers never move past each other while skipping non-alphanumeric characters. This not only prevents out-of-bounds access but also guarantees correct behavior even if no alphanumeric characters remain. In that case, the left pointer keeps advancing until it meets the right pointer and right does not move β€” both now pointing to the same non-alphanumeric character at the right. Since the comparison phase does not re-check whether characters are alphanumeric, the equality check at left === right counts as a valid match, moving both pointers inward. On the next iteration, left > right becomes true, ending the loop naturally and confirming the string as a valid palindrome.
  • Character comparison:
    Once both pointers are on valid characters:
    • If s[left] and s[right] differ (case-insensitive), the string cannot be a palindrome and we stop immediately.
    • If they match, move both pointers inward (left++, right--) to continue checking the next mirrored pair.
    This process checks all mirrored pairs directly in place, confirming the symmetry one step at a time.
  • Pointer crossing β€” natural termination:
    The loop continues as long as left < right. After validating the final matching pair (where both pointers may even point to the same position in the final check, as explained in the Core intuition section), the left and right pointers move past each other naturally. This crossing marks the successful completion of all comparisons β€” no mismatches were found, so the string is confirmed to be a valid palindrome.

Time Complexity

Let n be the length of the input string s. Each character is visited at most once, with only constant-time work per visit. Therefore, the total running time is O(n).

Space Complexity

The algorithm uses only two pointers, resulting in O(1) auxiliary space usage.

Visucodize editorial solution titled JS Solution for LeetCode's Valid Palindrome coding problem. Includes code and may include explanation. You can animate the code step by step using the provided input by clicking the run button, or fork it locally to update the code and input for custom visualization. View the original problem at https://leetcode.com/problems/valid-palindrome.