1. Filter, Lowercase & Reverse Check
Build a cleaned alphanumeric lowercase string buffer, then verify it equals its reverse.
Scans input once to filter and once to reverse/compare.
Allocates sanitized and reversed string copies of up to N chars.
How It Works
Direct Baseline Strategy
The problem statement defines a two-stage pipeline:
- Build a filtered buffer
cleanedappending lowercased alphanumeric characters froms. - Reverse
cleaned(or checkcleaned[i] == cleaned[n - 1 - i]) and compare.
While straightforward to write in ~4 lines of Python or JS, allocating cleaned and reversed copies costs O(n) auxiliary memory and scans twice.
Step-by-Step Dry Run
Filter "A man, a plan, a canal: Panama" into "amanaplanacanalpanama".
cleaned len = 21- +Compact and expressive in scripting languages
- +Easy to reason about before optimization
- -Allocates O(n) extra heap memory for filtered strings
- -Cannot short-circuit early before building the full cleaned string
Reference Implementation
Complete implementation for Filter, Lowercase & Reverse Check in Python, JavaScript, Java, C++, and C.
def is_palindrome_filter(s: str) -> bool:cleaned = [ch.lower() for ch in s if ch.isalnum()]return cleaned == cleaned[::-1]
function isPalindromeFilter(s) {
const cleaned = s.toLowerCase().replace(/[^a-z0-9]/g, "");
return cleaned === cleaned.split("").reverse().join("");
}public class Solution {
public static boolean isPalindromeFilter(String s) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isLetterOrDigit(c)) sb.append(Character.toLowerCase(c));
}
String forward = sb.toString();
return forward.equals(sb.reverse().toString());
}
}#include <string>
#include <cctype>
#include <algorithm>
bool isPalindromeFilter(const std::string& s) {
std::string cleaned;
for (unsigned char c : s) {
if (std::isalnum(c)) cleaned.push_back(static_cast<char>(std::tolower(c)));
}
std::string rev = cleaned;
std::reverse(rev.begin(), rev.end());
return cleaned == rev;
}#include <stdbool.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
bool isPalindromeFilter(const char* s) {
size_t n = strlen(s);
char* buf = (char*)malloc(n + 1);
size_t k = 0;
for (size_t i = 0; i < n; i++) {
if (isalnum((unsigned char)s[i])) buf[k++] = (char)tolower((unsigned char)s[i]);
}
bool ok = true;
for (size_t i = 0; i < k / 2; i++) {
if (buf[i] != buf[k - 1 - i]) { ok = false; break; }
}
free(buf);
return ok;
}