51. Echo After Cleanup

A voice app transcribes a spoken line into the string s, which may contain letters, digits, spaces and punctuation. The app wants to know whether the line is an echo: if you throw away every character that is not a letter or a digit and ignore the difference between upper and lower case, the remaining characters read the same from left to right and from right to left.

Return true if the cleaned line is an echo and false otherwise. A line with no letters or digits at all is considered an echo. Do it with two pointers that move toward each other, without building a cleaned copy of the string.

Example 1

Input:
s = "Never odd or even!"
Output:
true
Explanation:

Cleaned, the line is neveroddoreven, which reads the same both ways.

Example 2

Input:
s = "Echo? no echo"
Output:
false
Explanation:

Cleaned, the line is echonoecho, which reversed is ohceonohce, so it is not an echo.

Constraints

1 ≤ s.length ≤ 2 * 105

s contains printable ASCII characters

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…