Palindrome Detection in DSA Typescript - 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 many steps does it take to confirm the input reads the same forwards and backwards?
Analyze the time complexity of the following code snippet.
function isPalindrome(s: string): boolean {
let left = 0;
let 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, since it moves inward from both ends.
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 you only look at the first and last characters."
[OK] Correct: You need to compare pairs of characters moving inward, which grows with the string length, not just one pair.
Understanding how palindrome checking scales helps you explain your code clearly and shows you can analyze simple algorithms well.
"What if we only checked characters up to a quarter of the string length? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand palindrome definition
A palindrome is a word or phrase that reads the same forwards and backwards.Step 2: Compare options with definition
Only The string reads the same forwards and backwards matches this definition exactly.Final Answer:
The string reads the same forwards and backwards -> Option BQuick Check:
Palindrome = same forwards and backwards [OK]
- Confusing palindrome with uppercase or letter-only strings
- Thinking palindrome means no spaces
- Assuming palindrome means all lowercase
str is a palindrome?Solution
Step 1: Understand string reversal in TypeScript
To reverse a string, split it into characters, reverse the array, then join back.Step 2: Check each option's correctness
return str === str.split('').reverse().join(''); uses split(''), reverse(), join('') correctly. Others miss correct methods or arguments.Final Answer:
return str === str.split('').reverse().join(''); -> Option CQuick Check:
Correct string reversal syntax = return str === str.split('').reverse().join(''); [OK]
- Using reverse() directly on string (not an array)
- Using split() without arguments returns whole string as one element
- Forgetting to join array back to string
function isPalindrome(s: string): boolean {
const cleaned = s.toLowerCase().replace(/[^a-z0-9]/g, '');
return cleaned === cleaned.split('').reverse().join('');
}
console.log(isPalindrome('A man, a plan, a canal: Panama'));Solution
Step 1: Clean the string
The string is converted to lowercase and non-alphanumeric characters removed, resulting in 'amanaplanacanalpanama'.Step 2: Check palindrome condition
This cleaned string equals its reverse, so function returns true.Final Answer:
true -> Option AQuick Check:
Cleaned string equals reversed = true [OK]
- Not cleaning string before palindrome check
- Assuming spaces or punctuation affect palindrome
- Confusing true/false output
function checkPalindrome(str: string): boolean {
const reversed = str.split('').reverse();
return str === reversed;
}Solution
Step 1: Analyze reversed variable type
reversed is an array after split and reverse, not a string.Step 2: Compare string with array
Comparing string to array returns false always; need to join array back to string.Final Answer:
reversed is an array, not a string -> Option DQuick Check:
Compare string to string, not array [OK]
- Comparing string directly to array
- Forgetting to join reversed array
- Assuming split('') returns string
Solution
Step 1: Understand palindrome ignoring spaces and punctuation
We must clean the string by removing spaces and punctuation and normalize case.Step 2: Choose correct cleaning and comparison
Convert to lowercase, remove non-alphanumeric chars, then compare with reversed cleans string by removing non-alphanumeric characters and lowercases it before comparing with reversed.Final Answer:
Convert to lowercase, remove non-alphanumeric chars, then compare with reversed -> Option AQuick Check:
Clean and normalize before palindrome check [OK]
- Only removing spaces but not punctuation
- Comparing without case normalization
- Assuming uppercase conversion alone is enough
Ready to prove it?
Pass the test and this pattern moves unknown
