problems / 253

Is It a Palindrome After Cleanup?

easy strings two-pointers palindromes 1000 ms · 256 MB

Is It a Palindrome After Cleanup?

You are given several lines of text, such as Was it a car, or a cat I saw?.

For each line, keep only its letters (a–z, A–Z) and digits (0–9), throwing away spaces, punctuation and every other character, and treat upper-case and lower-case versions of a letter as the same. The line is a palindrome after cleanup if what remains reads the same forwards and backwards.

A line with no letters or digits at all leaves nothing behind, and counts as a palindrome.

Input

The first line contains an integer n (1 ≤ n ≤ 10^5), the number of lines.

Each of the next n lines contains one line of text. A line consists of printable ASCII characters (codes 33 to 126) and spaces (code 32), contains at least one character that is not a space, and does not start or end with a space.

The total length of all n lines (not counting line breaks) is at most 10^6. A single line can be up to 10^6 characters long.

Output

Print n lines. The i-th of them is YES if the i-th text line is a palindrome after cleanup, and NO otherwise.

Notes

In the first example, Was it a car, or a cat I saw? becomes wasitacaroracatisaw, which reads the same backwards. Hello, World! becomes helloworld, which does not. The last line has no letters or digits, so the answer is YES.

In the second example, digits are kept: Race car 2 becomes racecar2, which is not a palindrome. In 0 O the first character is the digit zero and the second is the letter O, so they differ.

Lines contain spaces, so read each one as a whole line rather than word by word.

Example 1

Input

4
Was it a car, or a cat I saw?
Hello, World!
No 'x' in Nixon
?! ... !?

Output

YES
NO
YES
YES

Example 2

Input

4
12:21
Race car 2
A1b, B1a
0 O

Output

YES
NO
YES
NO

Hints · reveal one at a time if you're stuck

Hint 1

Only letters and digits matter, and A and a count as the same character. Which characters of No 'x' in Nixon survive, and in what form?

Hint 2

Compare the first kept character with the last kept one, then move inward. Two indices, one starting at each end, can skip ignored characters on the fly, so each character is looked at once: this is the two-pointer technique.

report an issue Prefer your own editor? sooks prep 253 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.