Skip to the code
cf.
+25 XP

#608 Longest Mirrored Run in a Log Line

medium

A support tool flags strange log lines, and one signal is a long run of characters that reads the same forward and backward.

Write longestMirror(line) that returns the longest substring of line that is a palindrome. If several have the same longest length, return the one that starts first. For an empty line, return an empty string.

Example 1

Input: line = "xx_abcba-yy"
Output: "abcba"

abcba has length 5; nothing longer reads the same both ways.

Example 2

Input: line = "abcd"
Output: "a"

Every single letter is a palindrome of length 1; a starts first.

Constraints

  • 0 <= line.length <= 3000
  • line contains printable ASCII characters.
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).