Valid Palindrome Two Pointer in DSA C - Time & Space Complexity
We want to know how the time 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 input size increases?
Analyze the time complexity of the following code snippet.
bool isPalindrome(char * s) {
int left = 0, right = strlen(s) - 1;
while (left < right) {
if (s[left] != s[right]) {
return false;
}
left++;
right--;
}
return true;
}
This code checks if a string reads the same forwards and backwards using two pointers moving towards the center.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing characters at left and right pointers.
- How many times: Up to n/2 times, where n is the string length.
As the string gets longer, the number of comparisons grows roughly half as fast.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5 comparisons |
| 100 | 50 comparisons |
| 1000 | 500 comparisons |
Pattern observation: The steps grow linearly with input size, about half the length.
Time Complexity: O(n)
This means the time to check grows directly with the string length.
[X] Wrong: "The two-pointer method checks every character twice, so it is O(2n)."
[OK] Correct: Each character is checked only once in a pair, so the total checks are about n/2, which simplifies to O(n).
Understanding this linear time check helps you quickly validate strings in many problems, showing you can write efficient, clear code.
"What if we ignored non-alphanumeric characters and spaces? How would the time complexity change?"
Practice
Solution
Step 1: Understand two-pointer setup
Two pointers start at the beginning and end of the string to compare characters.Step 2: Move pointers inward while characters match
If characters at both pointers are equal, move pointers closer until they meet or cross.Final Answer:
Use two pointers starting from both ends and move towards the center comparing characters -> Option DQuick Check:
Two-pointer palindrome check = Use two pointers starting from both ends and move towards the center comparing characters [OK]
- Trying to sort the string which changes order
- Only checking half the string without pointers
- Ignoring non-alphanumeric characters if required
s?Solution
Step 1: Understand string indexing in C
Strings are zero-indexed, so first character is at 0 and last at length-1.Step 2: Correct pointer initialization
Left pointer starts at 0, right pointer at strlen(s)-1 to cover full string.Final Answer:
int left = 0, right = strlen(s) - 1; -> Option AQuick Check:
Indexing starts at 0, last index = length-1 [OK]
- Setting right pointer to strlen(s) which is out of bounds
- Starting left pointer at 1 skipping first character
- Using wrong indices causing runtime errors
char s[] = "racecar";
int left = 0, right = strlen(s) - 1;
int isPalindrome = 1;
while (left < right) {
if (s[left] != s[right]) {
isPalindrome = 0;
break;
}
left++;
right--;
}
printf("%d", isPalindrome);Solution
Step 1: Trace pointer comparisons on "racecar"
Characters at left and right match: 'r'=='r', 'a'=='a', 'c'=='c', 'e'=='e'.Step 2: Loop completes without mismatch
No mismatch found, so isPalindrome remains 1 and prints 1.Final Answer:
1 -> Option BQuick Check:
"racecar" is palindrome = 1 [OK]
- Assuming output is string instead of integer
- Confusing index bounds causing wrong comparisons
- Forgetting to break loop on mismatch
s is a palindrome using two pointers:int left = 0, right = strlen(s);
while (left <= right) {
if (s[left] != s[right]) {
return 0;
}
left++;
right--;
}
return 1;Solution
Step 1: Check right pointer initialization
Right pointer set to strlen(s) is out of bounds; should be strlen(s) - 1.Step 2: Check loop condition
Loop should run while left < right to avoid comparing same middle character twice.Final Answer:
Both A and B are bugs -> Option AQuick Check:
Right pointer and loop condition both incorrect = Both A and B are bugs [OK]
- Using strlen(s) as right pointer causing out-of-bounds access
- Using <= in loop causing unnecessary comparison
- Ignoring off-by-one errors
s that may contain uppercase letters, lowercase letters, and non-alphanumeric characters, which approach correctly checks if s is a palindrome ignoring cases and non-alphanumeric characters using two pointers?Solution
Step 1: Use two pointers and skip non-alphanumeric characters
Move left and right pointers inward, skipping characters that are not letters or digits.Step 2: Compare characters in lowercase form
Convert characters to lowercase before comparing to ignore case differences.Final Answer:
Use two pointers from ends, skip non-alphanumeric chars, compare lowercase characters -> Option CQuick Check:
Two-pointer with skip and lowercase = Use two pointers from ends, skip non-alphanumeric chars, compare lowercase characters [OK]
- Comparing characters without skipping non-alphanumeric
- Not converting characters to lowercase before comparison
- Using frequency count which doesn't check order
Ready to prove it?
Pass the test and this pattern moves unknown
