Longest Palindromic Substring
Real-world version: Longest Mirror RunThe statement here is our own: the same inputs, outputs and technique.
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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def longest_mirror_run(tag: str) -> str:public String longestMirrorRun(String tag)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Is SubsequenceReal-world version: Shorthand Fits the LabelEasy
- Palindromic SubstringsReal-world version: Reversible Tape StretchesMedium
- Find the Lexicographically Smallest Valid SequenceReal-world version: Cheapest Probe Slots On A ReelMedium
- Longest String ChainReal-world version: Longest Chain of Grown LabelsMedium