Palindrome Detection in DSA C++ - Time & Space Complexity
We want to know how the time to check if a word or phrase is a palindrome changes as the input gets longer.
How does the number of steps grow when the input size grows?
Analyze the time complexity of the following code snippet.
bool isPalindrome(const std::string &s) {
int left = 0;
int right = s.length() - 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 by comparing characters from both ends moving towards the center.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing characters at positions from the start and end of the string.
- How many times: Up to half the length of the string (n/2 times).
As the string gets longer, the number of character comparisons grows roughly half as fast.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5 comparisons |
| 100 | 50 comparisons |
| 1000 | 500 comparisons |
Pattern observation: The number of steps grows linearly with input size, but only half as many comparisons are needed.
Time Complexity: O(n)
This means the time to check a palindrome grows in direct proportion to the length of the string.
[X] Wrong: "Checking a palindrome takes constant time because we only compare a few characters."
[OK] Correct: The number of comparisons depends on the string length; longer strings need more checks, so time grows with input size.
Understanding how palindrome detection scales helps you explain your code clearly and shows you can analyze efficiency, a key skill in interviews.
"What if we changed the code to check only every other character? How would the time complexity change?"
Practice
Solution
Step 1: Understand palindrome definition
A palindrome is a word or phrase that reads the same from left to right and right to left.Step 2: Compare options with definition
Only "A string that reads the same forwards and backwards" matches this meaning exactly.Final Answer:
A string that reads the same forwards and backwards -> Option BQuick Check:
Palindrome = same forwards and backwards [OK]
- Confusing palindrome with unique characters
- Thinking palindrome means only vowels
- Assuming palindrome ends with special character
s is a palindrome?Solution
Step 1: Analyze loop and index usage
for (int i = 0; i < s.size() / 2; i++) if (s[i] != s[s.size() - i - 1]) return false; correctly loops till half the string and compares characters from start and end.Step 2: Check correctness of index calculations
for (int i = 0; i < s.size() / 2; i++) if (s[i] != s[s.size() - i - 1]) return false; usess.size() - i - 1which is correct for zero-based indexing.Final Answer:
for (int i = 0; i < s.size() / 2; i++) if (s[i] != s[s.size() - i - 1]) return false; -> Option AQuick Check:
Correct index and loop range = for (int i = 0; i < s.size() / 2; i++) if (s[i] != s[s.size() - i - 1]) return false; [OK]
- Using wrong loop range (full length instead of half)
- Incorrect index calculation causing out-of-range errors
- Comparing adjacent characters instead of mirrored ones
std::string s = "racecar";
bool isPalindrome = true;
for (int i = 0; i < s.size() / 2; i++) {
if (s[i] != s[s.size() - i - 1]) {
isPalindrome = false;
break;
}
}
std::cout << (isPalindrome ? "Yes" : "No") << std::endl;Solution
Step 1: Understand the string and loop
The string "racecar" is a palindrome because it reads the same forwards and backwards.Step 2: Trace the loop comparisons
The loop compares characters from start and end; all pairs match, soisPalindromeremains true.Final Answer:
Yes -> Option AQuick Check:
"racecar" is palindrome = Yes [OK]
- Assuming "racecar" is not palindrome
- Confusing output strings
- Ignoring break statement in loop
bool checkPalindrome(std::string s) {
for (int i = 0; i <= s.size() / 2; i++) {
if (s[i] != s[s.size() - i]) {
return false;
}
}
return true;
}Solution
Step 1: Check loop range and indexing
The end index s[s.size() - i] for i=0 accesses s[s.size()], causing out-of-range error.Step 2: Correct index calculation
Change to s[s.size() - i - 1] for proper zero-based end index mirroring.Final Answer:
End index should be s[s.size() - i - 1] instead of s[s.size() - i] to avoid out-of-range -> Option DQuick Check:
End index = size - i - 1 [OK]
- Wrong end index s[size - i] causes out-of-bound access
- Confusing equality and inequality in comparisons
- Ignoring return type correctness
Solution
Step 1: Understand palindrome ignoring spaces and punctuation
To ignore spaces and punctuation, remove all non-alphanumeric characters and convert to lowercase for uniform comparison.Step 2: Apply palindrome check on cleaned string
After cleaning, check if the string reads the same forwards and backwards.Final Answer:
Remove non-alphanumeric characters, convert to lowercase, then check palindrome -> Option CQuick Check:
Clean string then check palindrome [OK]
- Checking palindrome without cleaning string
- Ignoring case differences
- Assuming spaces and punctuation don't affect palindrome
Ready to prove it?
Pass the test and this pattern moves unknown
