Drill

ProblemsC# › patterns

Mirror text, ignoring the clutter

easypatternsTwo pointersStringsC#

An audit tool checks whether a phrase reads the same forwards and backwards once the formatting — spaces, punctuation, digits — is stripped away.

IsCleanPalindrome(text: string) → bool

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public bool IsCleanPalindrome(string text) {
    
}

Worked examples

CallResult
IsCleanPalindrome("race a car")false
IsCleanPalindrome("A man, a plan, a canal: Panama")true
IsCleanPalindrome("racecar")true
IsCleanPalindrome(" ")true

Hint

Walk from both ends at once, skipping anything that is not a letter or digit, and compare the survivors.

Reference solution in C#
public bool IsCleanPalindrome(string text) {
    int i = 0, j = text.Length - 1;
    while (i < j) {
        char a = text[i], b = text[j];
        if (!char.IsLetterOrDigit(a)) { i++; continue; }
        if (!char.IsLetterOrDigit(b)) { j--; continue; }
        if (char.ToLowerInvariant(a) != char.ToLowerInvariant(b)) return false;
        i++; j--;
    }
    return true;
}

The same problem in another language

More patterns problems in C#