Loading...
Loading...
A college quiz club made a word game. A player wins a round if the given word reads the same from left to right and from right to left after deleting at most one letter. Deleting nothing is allowed. Print YES if the word can be made like this, otherwise print NO.
YES or NO.Example 1
Input
abca
Output
YES
Delete the b to get aca, or the c to get aba. Both read the same both ways.
Example 2
Input
abc
Output
NO
Deleting any one letter leaves bc, ac or ab. None of them reads the same both ways.
Try the problem first. Open one hint at a time.
Compare letters from both ends, moving inward while they match.
At the first pair that does not match, one of these two letters must be deleted.
You do not know which one, so try both: check if the part without the left letter reads the same both ways, and check the part without the right letter.
Each check is a simple two-index scan, so the whole thing takes linear time.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor