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).
Input: text = "xabacabay" Output: [1,7]
abacaba starts at index 1 and has length 7.
Input: text = "abcd" Output: [0,1]
No two neighbors match, so the best is a single letter; the first one starts at 0.
0 <= text.length <= 2 * 10^5text contains lowercase letters.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).
I'm here to help with #610 Longest Mirror in a Huge Transcript. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.