All problems
0010MediumTwo PointersStringDynamic Programming

Longest Mirror Run

Tracked in this browser only
Write code

Trains the technique from

LeetCode 5Longest Palindromic Substring

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A firmware pipeline stamps every build artifact with an alphanumeric label tag, made up only of digits and English letters.

Call a contiguous slice of tag a mirror run when the slice is identical to itself written back to front. Characters are compared exactly as they appear, so T and t never match.

Return the longest mirror run contained in tag. When two or more mirror runs are tied for longest, hand back the one whose first character sits at the lowest index.

Examples

Example 1

Input
tag = "kayak7pop"
Output
"kayak"

The slice `kayak` covers indices 0 through 4 and mirrors itself; `pop` also mirrors but is shorter.

Example 2

Input
tag = "9m4tt4z"
Output
"4tt4"

Here the widest mirror run has even width, sitting between the two `t` characters.

Example 3

Input
tag = "aA"
Output
"a"

Case matters, so the two characters do not mirror each other and the earliest single character wins.

Constraints

  • 1 <= tag.length <= 1000
  • tag contains digits and English letters only (both cases allowed)
  • Ties at the maximum width are broken by the lowest starting index

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def longest_mirror_run(tag: str) -> str:
Java
public String longestMirrorRun(String tag)
September 7
Apply