Problem overview
The method IsPalindrome takes a string, input, and returns a bool: true if the string reads the same forwards and backwards, false otherwise. Spaces count as characters, so "madam racecar madam" is a palindrome (the answer is true), while "abcbx" is not (the answer is false).
Approach: reverse, then compare
The definition of a palindrome gives the algorithm directly. If a string is equal to its own reverse, it is a palindrome. So build the reversed string, then compare it with the original.
Building the reversal takes one pass over the characters, and the comparison takes one more, so the whole method is linear in the length of the string. The cost is memory: the reversed copy holds as many characters as the input, so this version uses O(n) extra space. The two-pointer idea from the hints, which compares input[i] with input[j] while moving the indexes toward each other, needs only O(1) extra space. This lesson takes the shorter route of a reversed copy first, and the two-pointer version is a natural follow-up.
Step by step
- Build the reversed string. Strings have no
Reversemethod of their own. LINQ provides one for any sequence, and a string is a sequence ofcharvalues, soinput.Reverse()yields the characters from last to first.ToArray()collects them into achar[], andnew string(...)turns that array into a string. - Compare and return.
input == reversedInputis already abool, so it can be returned directly without anif.
The solution in C#
class Solution {
public bool IsPalindrome(string input) {
string reversedInput = new string(input.Reverse().ToArray());
return input == reversedInput;
}
}
The C# details behind these two lines:
- LINQ's
Reverse. It is an extension method from theSystem.Linqnamespace, so a file needsusing System.Linq;to call it. It doesn't modify the string; it produces a sequence of characters in reverse order, evaluated asToArray()walks through it. - Why
new string(...).ToArray()returns achar[], not astring, and thestringconstructor accepts achar[]. CallingToString()on the array would give you the type name instead of the text. ==compares contents. For two strings,==checks whether they contain the same characters in the same order, not whether they are the same object in memory. The comparison is case-sensitive.- Returning a comparison. Writing
if (input == reversedInput) return true; else return false;works but is longer; the expression itself is the answer.
Complexity
Time: O(n). Producing the reversed characters, collecting them into an array, creating the string and comparing the two strings each take one pass over n characters. A constant number of linear passes is still linear.
Space: O(n). The array and the new string each hold n characters. An O(1)-space version needs the two-pointer comparison, which checks the string against itself without making a copy.
Edge cases and common mistakes
- Empty string. It equals its own reverse, so the result is
true. - One character. Always a palindrome.
- Case.
"Aba"returnsfalsebecause'A'and'a'are different characters. If a problem wanted case to be ignored, you would lower-case both sides first. - Spaces and punctuation. They are compared like any other character, so
"A man"is not a palindrome. - Missing
using System.Linq;. Without it the call toReverse()on a string doesn't compile. - Comparing with
ReferenceEquals. That asks whether two variables point to the same object, and a freshly built reversed string never does, so it would returnfalseeven for"madam".
Testing
"madam racecar madam"givestrue."abcbx"givesfalse."ab"givesfalse."a"givestrue.""givestrue.