Palindrome Detection in DSA C# - Time & Space Complexity
We want to understand how the time needed to check if a word or phrase is a palindrome changes as the input gets longer.
The question is: how does the number of steps grow when the input size grows?
Analyze the time complexity of the following code snippet.
public bool IsPalindrome(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 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: "The palindrome check takes constant time because it just compares characters."
[OK] Correct: The code compares pairs of characters for about half the string length, so the time grows as the string gets longer, not fixed.
Understanding this time complexity helps you explain how efficient your palindrome check is and compare it to other string operations.
"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 forwards and backwards, ignoring spaces and punctuation.Step 2: Apply definition to options
A string that reads the same forwards and backwards matches this definition exactly, while others describe different string properties.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 empty string is always palindrome
s is a palindrome?Solution
Step 1: Understand palindrome check logic
To check palindrome, compare string with its reversed version.Step 2: Analyze each option
return s == new string(s.Reverse().ToArray()); reverses string and compares correctly. return s == s.ToUpper(); compares uppercase, not palindrome. return s.Length == 0; checks empty string only. return s[0] == s[s.Length]; accesses invalid index (out of range).Final Answer:
return s == new string(s.Reverse().ToArray()); -> Option CQuick Check:
Reverse and compare = return s == new string(s.Reverse().ToArray()); [OK]
- Using wrong index causing out of range error
- Comparing uppercase instead of reversed string
- Checking only empty string
string s = "level"; bool isPalindrome = s == new string(s.Reverse().ToArray()); Console.WriteLine(isPalindrome);
Solution
Step 1: Reverse the string "level"
Reversed string is "level", same as original.Step 2: Compare original and reversed strings
Since both are equal, isPalindrome is true, so Console.WriteLine prints True.Final Answer:
True -> Option AQuick Check:
"level" reversed = "level" [OK]
- Assuming output is False for palindromes
- Confusing compilation error due to Reverse()
- Thinking runtime exception occurs
string s = "test";
bool isPalindrome = true;
for (int i = 0; i <= s.Length / 2; i++)
{
if (s[i] != s[s.Length - i])
{
isPalindrome = false;
break;
}
}
Console.WriteLine(isPalindrome);Solution
Step 1: Identify index error in character comparison
String indices go from 0 to s.Length - 1, so s[s.Length - i] is out of range when i=0.Step 2: Fix index to s.Length - i - 1
This correctly accesses the mirrored character from the end.Final Answer:
Change s.Length - i to s.Length - i - 1 -> Option DQuick Check:
Correct index fixes out of range error [OK]
- Using s.Length - i instead of s.Length - i - 1
- Looping beyond half the string length
- Removing break causing unnecessary checks
Solution
Step 1: Clean string by removing non-letter/digit and convert to lowercase
string clean = new string(s.Where(char.IsLetterOrDigit).ToArray()).ToLower(); bool isPal = clean == new string(clean.Reverse().ToArray()); uses Where with char.IsLetterOrDigit and ToLower to normalize string.Step 2: Reverse cleaned string and compare
string clean = new string(s.Where(char.IsLetterOrDigit).ToArray()).ToLower(); bool isPal = clean == new string(clean.Reverse().ToArray()); reverses cleaned string and compares correctly for palindrome ignoring case and spaces.Final Answer:
string clean = new string(s.Where(char.IsLetterOrDigit).ToArray()).ToLower(); bool isPal = clean == new string(clean.Reverse().ToArray()); -> Option AQuick Check:
Normalize then reverse and compare = string clean = new string(s.Where(char.IsLetterOrDigit).ToArray()).ToLower(); bool isPal = clean == new string(clean.Reverse().ToArray()); [OK]
- Not removing spaces or punctuation
- Comparing original string without normalization
- Using uppercase comparison incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
