A research submarine logs every sonar pulse it receives as a single lowercase letter, building up one long string s of pulse codes in the order they arrive. The crew calls any two consecutive pulses in the log a blip pair. They are hunting for a mirror echo: a blip pair that appears somewhere in the log such that the same two pulses, read in the opposite order, also appear as a blip pair somewhere in the log. The two occurrences may overlap (for example, aba contains both ab and ba, sharing the middle letter), and a blip pair made of two identical pulses, such as ss, automatically counts as its own mirror the instant it appears once. Given the full pulse log, determine whether it contains a mirror echo.
s, made only of lowercase English letters.YES if s contains a mirror echo, or NO otherwise.1 <= length of s <= 100000Example 1
Input
radar
Expected
YES
Explanation
The adjacent blip pairs in `radar` are `ra`, `ad`, `da`, `ar`. The pair `ar` (the last two letters) is the exact reverse of the pair `ra` (the first two letters), and both appear in the log, so this is a mirror echo: `YES`.
Example 2
Input
sonar
Expected
NO
Explanation
The adjacent blip pairs in `sonar` are `so`, `on`, `na`, `ar`. Checking each pair's reverse — `os`, `no`, `an`, `ra` — none of them matches any pair actually present in the log, so there is no mirror echo: `NO`.
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 →