Tutorialspoint
Problem
Solution
Submissions

If String is a Rotation of Another

Certification: Intermediate Level Accuracy: 0% Submissions: 0 Points: 10

Write a C# program to check if a given string is a palindrome without using any built-in string reversal functions like Reverse(), or any LINQ methods. A palindrome is a word, phrase, or sequence that reads the same backward as forward. For this problem, consider only alphanumeric characters and ignore case sensitivity.

Example 1
  • Input: str = "racecar"
  • Output: true
  • Explanation:
    • Convert the string to lowercase: "racecar".
    • Compare characters from both ends: 'r' == 'r' (positions 0 and 6).
    • Move inward: 'a' == 'a' (positions 1 and 5).
    • Continue: 'c' == 'c' (positions 2 and 4).
    • Reach the middle character 'e' (position 3).
    • All comparisons match, so "racecar" is a palindrome.
Example 2
  • Input: str = "hello"
  • Output: false
  • Explanation:
    • Convert the string to lowercase: "hello".
    • Compare characters from both ends: 'h' != 'o' (positions 0 and 4).
    • Since the first comparison fails, "hello" is not a palindrome.
    • No need to check further as we already found a mismatch.
Constraints
  • 1 ≤ str.length ≤ 10^4
  • str contains only alphanumeric characters and spaces
  • You are not allowed to use built-in string reversal functions or LINQ
  • Case should be ignored (treat 'A' and 'a' as the same)
  • Time Complexity: O(n)
  • Space Complexity: O(1)
StringsPhillipsOracle
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use two pointers approach - one starting from the beginning and one from the end
  • Convert characters to lowercase for case-insensitive comparison
  • Skip non-alphanumeric characters if present
  • Compare characters at both pointers and move them toward each other
  • If any comparison fails, return false immediately
  • If all comparisons pass, return true

Steps to solve by this approach:

 Step 1: Create a clean version of the input string by removing spaces and non-alphanumeric characters, and convert all characters to lowercase.
 Step 2: Initialize two pointers - left starting at the beginning (index 0) and right starting at the end (last index) of the cleaned string.
 Step 3: Enter a while loop that continues as long as the left pointer is less than the right pointer.
 Step 4: Compare the characters at the left and right positions in the cleaned string.
 Step 5: If the characters don't match, immediately return false as the string is not a palindrome.
 Step 6: If the characters match, increment the left pointer and decrement the right pointer to move toward the center.
 Step 7: If the loop completes without finding any mismatches, return true as the string is a palindrome.

Submitted Code :