Skip to the code
cf.
+50 XP

#610 Longest Mirror in a Huge Transcript

hard

A text-analysis feature runs in the browser on pasted transcripts of up to 200,000 characters. One of its stats is the longest palindromic substring, and the page must stay responsive.

Write longestMirrorSpan(text) that returns [start, length] for the longest substring that reads the same forward and backward. If several share the longest length, use the one with the smallest start. For an empty text, return [0, 0].

An O(n^2) approach is too slow for the largest inputs; aim for O(n).

Example 1

Input: text = "xabacabay"
Output: [1,7]

abacaba starts at index 1 and has length 7.

Example 2

Input: text = "abcd"
Output: [0,1]

No two neighbors match, so the best is a single letter; the first one starts at 0.

Constraints

  • 0 <= text.length <= 2 * 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).