Problem 346724 · medium · Phase 03 Linear Management & Searching

Longest Mirrored Stretch

strings · palindromes · expand around centre

A DNA lab flags "mirrored" stretches: substrings that read the same forwards and backwards. Given a string s, return the longest such substring. If several have the maximum length, return the one that starts first. For an empty string return "".

Examples

Input:  s = "abacdc"
Output: "aba"
Explanation: "aba" and "cdc" both have length 3; "aba" starts first.

Input:  s = "xyz"
Output: "x"

Constraints

  • 0 <= len(s) <= 2000, lowercase letters only
  • Target: O(n^2) time, O(1) extra space.

Goals

  • Enumerate both odd and even palindrome centres
  • Expand outwards while the characters match
  • Track the earliest longest result
Starting Python…