The iterative approach is more memory-efficient and faster for large inputs, while the recursive approach is more elegant and useful for smaller inputs or learning recursion.
Key takeaways:
Using two pointers to traverse the string is faster and more memory-efficient, especially for large inputs.
This exercise strengthens core string manipulation techniques, like filtering, traversing, and logical validation.
Recursive palindrome checks are concise and intuitive for small strings, offering a clear view of the problem's structure.
Handling inputs like empty strings and single characters ensures your solution is comprehensive and reliable.
A palindrome is a word, phrase, or sequence of characters that reads the same backward as forward, ignoring case, spaces, and punctuation. Examples include words like “radar,” “level,” “civic,” “deified,” and “noon,” which demonstrate symmetry by appearing identical when read in either direction. Numeric palindromes like “12321” and “45654” also maintain their integrity when their digits are reversed. Checking if a string is a palindrome is a common problem in programming, and C# provides efficient tools to solve it. In this Answer, we’ll explore step-by-step methods to check for palindromes in C#.
When determining whether a string is a palindrome in C#, there are multiple methods to consider. In this discussion, we will focus on two core strategies: the iterative approach and the recursive approach.
The iterative approach uses two pointers, one starting at the beginning of the string and the other at the end. These pointers move toward the center, comparing characters at each step. If all corresponding characters match, the string is a palindrome.
In contrast, the recursive approach relies on a function that compares characters at the opposite ends of the string, then moves inward by recursively calling itself to validate the remaining substring. Both techniques are effective and provide unique perspectives on evaluating palindromes in C#.
The iterative approach uses a straightforward loop to evaluate whether a string is a palindrome. It employs two pointers: one starting at the beginning of the string and the other at the end. The pointers move toward the center of the string, comparing characters at each step. If all characters match, the string is a palindrome. If any mismatch occurs, the process terminates early, confirming that the string is not a palindrome.
Initialize pointers: Define two pointers: left
starts at the beginning of the string (left = 0
), and right
starts at the end of the string (right = str.Length - 1
).
Compare characters: Check if the characters at the positions str[left]
and str[right]
are equal.
Move pointers: If the characters match, increment the left
pointer by 1 (left++
) to move it forward and decrement the right
pointer by 1 (right--
) to move it backward.
Mismatch check: If the characters at any point do not match, return false
, as the string is not a palindrome.
Repeat comparison: Continue steps 2–4 until the left
pointer meets or crosses the right
pointer (left >= right
).
Completion: If the loop completes without encountering any mismatches, return true
, confirming that the string is a palindrome.
Following is the implementation of the steps discussed above:
using System;class Program{// Method to check if a string is a palindrome using an iterative approachstatic bool IsPalindromeIterative(string str){int left = 0, right = str.Length - 1;// Compare characters from both ends moving towards the centerwhile (left < right){if (str[left] != str[right])return false; // Return false if any characters don't matchleft++; // Move left pointer inwardright--; // Move right pointer inward}return true; // Return true if all characters match}static void Main(){// Define a list of test cases including valid and invalid palindromesstring[] testCases = {"civic", // Valid palindrome"radar", // Valid palindrome"level", // Valid palindrome"deified", // Valid palindrome"12321", // Numeric palindrome"hello", // Not a palindrome"world", // Not a palindrome"Palindrome", // Not a palindrome"madam", // Valid palindrome"noon" // Valid palindrome};// Iterate through each test case and check if it's a palindromeforeach (var testCase in testCases){bool isPalindrome = IsPalindromeIterative(testCase);// Output formatted resultConsole.WriteLine($"Checking: {testCase}");Console.WriteLine(isPalindrome? "Result: The given string is a palindrome.": "Result: The given string is not a palindrome.");Console.WriteLine(new string('-', 100)); // Print a dotted line separator}}}
The time complexity of the iterative solution is
In terms of space complexity, the iterative solution is highly efficient, requiring left
and right
) to keep track of indexes.
Advantages of using two pointers:
For the iterative approach, directly compare characters in the string without creating new strings (e.g., reversing or slicing substrings) for large inputs. This can save memory and reduce runtime.
The recursive approach solves the problem by comparing characters at opposite ends of the string and recursively validating the substring between those characters. If the string is empty or has only one character (base cases), it is a palindrome. Otherwise, it checks the first and last characters, and if they match, calls the function again with the substring excluding those two characters.
Define a base case: Check if the left
index is greater than or equal to the right
index (left >= right
). This means the string is either empty or has one character left, indicating it is a palindrome. Return true
in this case.
Compare characters: Compare the characters at the current left
and right
indexes (str[left]
and str[right]
). If the characters do not match, return false
, as this means the string is not a palindrome.
Make a recursive call: If the characters match, make a recursive call to IsPalindromeRecursive
, incrementing the left
index by 1 (left + 1
) and decrementing the right
index by 1 (right - 1
). This effectively checks the next inner substring.
Terminate on mismatch: If any recursive call encounters a mismatch in characters, it will return false
, propagating this result back up the call stack.
Complete recursion: If the recursion completes successfully without any mismatches, the method returns true
, confirming that the string is a palindrome.
Following is the implementation of the steps discussed above:
using System;class Program{// Recursive method to check if a string is a palindrome.static bool IsPalindromeRecursive(string str, int left, int right){// Base case: If left index meets or exceeds the right, it's a palindromeif (left >= right){return true;}// Check if the characters at the current indices are not equalif (str[left] != str[right]){return false; // Not a palindrome}// Recursive call with next inner indicesreturn IsPalindromeRecursive(str, left + 1, right - 1);}static void Main(){// Test cases with valid and invalid palindromesstring[] testCases = {"deified", // Valid palindrome"radar", // Valid palindrome"level", // Valid palindrome"civic", // Valid palindrome"madam", // Valid palindrome"12321", // Numeric palindrome"hello", // Not a palindrome"world", // Not a palindrome"Palindrome", // Not a palindrome"noon" // Valid palindrome};// Check each test caseforeach (var testCase in testCases){// Check if the string is a palindromebool isPalindrome = IsPalindromeRecursive(testCase, 0, testCase.Length - 1);// Output formatted resultConsole.WriteLine($"Checking: {testCase}");Console.WriteLine(isPalindrome? "Result: The given string is a palindrome.": "Result: The given string is not a palindrome.");Console.WriteLine(new string('-', 100));}}}
The recursive solution has a time complexity of
However, the space complexity of the recursive solution is
Both approaches are valid and provide valuable insights into solving palindrome problems in C#. Choose the one that fits your use case and personal preference!
Feature | Iterative Approach | Recursive Approach |
Ease of Use | Straightforward | Slightly complex |
Performance | More efficient | More memory overhead |
Suitability | Best for larger strings | Best for conceptual clarity |
Implementation | Simple loop with pointers | Requires understanding of recursion |
Haven’t found what you were looking for? Contact Us
Free Resources