Valid Palindrome Two Pointer in DSA Javascript - Time & Space Complexity
We want to know how the time needed to check if a string is a palindrome grows as the string gets longer.
Specifically, how many steps does the two-pointer method take as the input size increases?
Analyze the time complexity of the following code snippet.
function isPalindrome(s) {
let left = 0;
let right = s.length - 1;
while (left < right) {
while (left < right && !isAlphaNum(s[left])) left++;
while (left < right && !isAlphaNum(s[right])) right--;
if (s[left].toLowerCase() !== s[right].toLowerCase()) return false;
left++;
right--;
}
return true;
}
function isAlphaNum(c) {
return /[a-z0-9]/i.test(c);
}
This code checks if a string is a palindrome by moving two pointers from the ends towards the center, skipping non-alphanumeric characters.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves two pointers from the start and end towards the middle.
- How many times: Each character is visited at most once by either pointer, so up to n times where n is the string length.
As the string gets longer, the number of steps grows roughly in a straight line with the string length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The steps increase directly with input size, doubling the input roughly doubles the work.
Time Complexity: O(n)
This means the time to check grows in a straight line with the length of the string.
[X] Wrong: "Because there are nested while loops, the time must be quadratic O(n²)."
[OK] Correct: The inner loops only skip non-alphanumeric characters and each character is visited once total, so the total work is still linear.
Understanding this linear time check shows you can analyze pointer-based string algorithms clearly, a useful skill for many coding challenges.
"What if we removed the checks that skip non-alphanumeric characters? How would the time complexity change?"
Practice
Solution
Step 1: Understand the two-pointer approach
Two pointers start at the beginning and end of the string and move inward.Step 2: Compare characters at pointers
If characters at both pointers match, move pointers closer; if not, string is not palindrome.Final Answer:
Use two pointers starting from both ends and move towards the center comparing characters -> Option AQuick Check:
Two-pointer technique = Use two pointers starting from both ends and move towards the center comparing characters [OK]
- Trying to reverse the string instead of using pointers
- Checking only half the string without comparing both ends
- Ignoring case or non-alphanumeric characters
Solution
Step 1: Identify correct pointer initialization
Left pointer should start at 0 (start of string), right pointer at last index (length - 1).Step 2: Check each option
let left = 0, right = s.length - 1; correctly sets left=0 and right=s.length-1; others have incorrect indices.Final Answer:
let left = 0, right = s.length - 1; -> Option DQuick Check:
Start pointers at 0 and length-1 = let left = 0, right = s.length - 1; [OK]
- Setting right pointer to s.length (out of bounds)
- Swapping left and right pointers
- Starting left pointer at 1 instead of 0
function isPalindrome(s) {
let left = 0, right = s.length - 1;
while (left < right) {
if (s[left] !== s[right]) return false;
left++;
right--;
}
return true;
}
console.log(isPalindrome('racecar'));
console.log(isPalindrome('hello'));Solution
Step 1: Trace isPalindrome('racecar')
Characters match from both ends moving inward, so returns true.Step 2: Trace isPalindrome('hello')
Characters at start and end differ ('h' vs 'o'), returns false immediately.Final Answer:
true false -> Option AQuick Check:
'racecar' palindrome = true, 'hello' not palindrome = false [OK]
- Assuming 'hello' is palindrome
- Not returning false immediately on mismatch
- Confusing return values order
function isPalindrome(s) {
let left = 0, right = s.length;
while (left < right) {
if (s[left] !== s[right]) return false;
left++;
right--;
}
return true;
}Solution
Step 1: Check pointer initialization
Right pointer is set to s.length, which is out of string index range (0 to length-1).Step 2: Correct pointer initialization
Right pointer should be s.length - 1 to point to last character.Final Answer:
Right pointer should start at s.length - 1, not s.length -> Option CQuick Check:
Right pointer = length - 1 [OK]
- Using s.length as index causes undefined access
- Changing left pointer start incorrectly
- Incorrect loop condition causing infinite loop
s = 'A man, a plan, a canal: Panama', which modification to the two-pointer palindrome check is needed to correctly identify it as a palindrome?Solution
Step 1: Understand the input string
The string contains spaces, commas, and mixed cases which should be ignored for palindrome check.Step 2: Modify two-pointer approach
Skip non-alphanumeric characters and compare characters in lowercase to handle case differences.Final Answer:
Ignore non-alphanumeric characters and compare characters case-insensitively -> Option BQuick Check:
Clean and lowercase string before comparing [OK]
- Comparing characters without ignoring symbols
- Not handling uppercase vs lowercase differences
- Assuming direct comparison works on raw string
Ready to prove it?
Pass the test and this pattern moves unknown
