A sign-maker wants to know whether a slogan reads the same forwards and backwards once you ignore everything except letters and digits, and ignore upper/lower case.
Write reads_same_both_ways(s: str) -> bool.
- Only the characters
a-z,A-Zand0-9count; spaces, punctuation and every other symbol are skipped. "A"and"a"are the same letter. Digits must match exactly.- A string with no letters or digits at all (including the empty string) counts as reading the same both ways.
reads_same_both_ways("Step on no pets!") # True
reads_same_both_ways("Top spot, not a pot") # False
reads_same_both_ways("No 'x' in Nixon") # True
reads_same_both_ways("1a2") # False
reads_same_both_ways(" ,.;") # True
Constraints: 0 <= len(s) <= 2 * 10^5; s contains printable ASCII characters only.
Aim for O(n) time and O(1) extra space: don't build a cleaned-up copy of the string.
Show hint
compare characters from both ends at once, and let each end skip over the characters that don't count before comparing.