Skip to the code
cf.
+25 XP

#612 Mirror Reach at Every Letter

medium

A text editor plugin shows, for each character, how long the mirrored text centered on it is, as a little heat map.

Write mirrorLengths(text) that returns an array where entry i is the length of the longest odd-length palindrome centered on text[i]. Every entry is at least 1.

Text can be 100,000 characters long, and text like aaaa... makes growing from every letter one step at a time far too slow.

Example 1

Input: text = "abacaba"
Output: [1,3,1,7,1,3,1]

Around c the whole word mirrors (7); around each b, aba does (3).

Example 2

Input: text = "aaaa"
Output: [1,3,3,1]

The middle two letters each have aaa around them.

Constraints

  • 0 <= text.length <= 10^5
  • text contains lowercase letters.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).