A museum floor is tiled with a single row of n tiles (n is always even), and each tile carries a lowercase letter code identifying its color pattern. The original design was a perfect mirror image: reading the row from the left end should exactly match reading it from the right end, tile for tile. Over the years, some tiles were replaced with the wrong pattern. A restorer can re-lay any tile with any single lowercase letter of their choosing, one tile per operation. Given the current row of letters, determine whether the restorer can recover full mirror symmetry using at most two individual tile replacements in total.
Print true if at most two tile replacements suffice to make s read the same forwards and backwards (a palindrome), and false otherwise.
Example 1
Input
6 abcxya
Expected
true
Explanation
Comparing mirrored pairs: (s[0],s[5])=('a','a') match, (s[1],s[4])=('b','y') mismatch, (s[2],s[3])=('c','x') mismatch. There are exactly 2 mismatched pairs, and each can be fixed with one replacement, so the answer is true.
Example 2
Input
6 abcdef
Expected
false
Explanation
Comparing mirrored pairs: (s[0],s[5])=('a','f') mismatch, (s[1],s[4])=('b','e') mismatch, (s[2],s[3])=('c','d') mismatch. There are 3 mismatched pairs, which needs 3 replacements, more than the allowed 2, so the answer is false.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →