Drill

ProblemsJava › patterns

Mirror text, ignoring the clutter

easypatternsTwo pointersStringsJava

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

Java 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

boolean 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 Java
boolean isCleanPalindrome(String text) {
    int i = 0, j = text.length() - 1;
    while (i < j) {
        char a = text.charAt(i), b = text.charAt(j);
        if (!Character.isLetterOrDigit(a)) { i++; continue; }
        if (!Character.isLetterOrDigit(b)) { j--; continue; }
        if (Character.toLowerCase(a) != Character.toLowerCase(b)) return false;
        i++; j--;
    }
    return true;
}

The same problem in another language

More patterns problems in Java