Let's break down the classic and efficient implementation using the two-pointer approach to solve the valid palindrome problem.
The core idea is to iterate over the string from both ends towards the center, comparing characters that are alphanumeric while ignoring non-alphanumeric characters and case differences. If any mismatch is found, it's not a palindrome; otherwise, it is.
- Two-Pointer Technique:The
leftIndex
starts at the beginning of the string (index 0), and therightIndex
starts at the end (indexs.length() - 1
). These pointers move towards each other. - Normalization On-the-Fly: Instead of creating a new normalized string, we convert characters to lowercase and
checks their alphanumeric status on-the-fly as it encounters them.
- If the character at
leftIndex
orrightIndex
is not alphanumeric, we skip it by moving the respective pointer (leftIndex++
orrightIndex--
). - If both characters are alphanumeric, we compare them. If they are not equal, we return
false
immediately, indicating that the string is not a palindrome. - If they are equal, both pointers are moved towards the center (
leftIndex++
andrightIndex--
), and the process continues.
- If the character at
- Loop Termination: The loop continues until
leftIndex
is no longer less thanrightIndex
, meaning the entire string has been checked.
- Time Complexity: O(N). Each character in the string is visited at most once. The operations within the loop (character comparison, case conversion, and alphanumeric check) are O(1), leading to a linear time complexity.
- Space Complexity: O(1). Our solution does not require extra space proportional to the input size (no additional strings or data structures are used). The space used is constant, regardless of the input string size.
This solution is concise, efficient, and leverages the two pointers technique effectively. It handles the palindrome check in-place without needing additional space for a normalized string, making it an optimal solution in terms of both time and space complexity.