Ctrl K

Longest Mirror Fragment

Medium
You are given a string text. Find its longest contiguous piece that reads the same forwards and backwards, and return that piece. Uppercase and lowercase letters count as different characters. The input is guaranteed to have exactly one longest such piece.
Example 1
text="xlevelx9" → 'xlevelx'

The longest palindromic piece is xlevelx, seven characters long.

Example 2
text="noonday" → 'noon'

noon reads the same both ways, and there is no longer palindrome.

Example 3
text="Abba7" → 'bb'

Case matters: A and a are different characters, so Abba is not a palindrome. That leaves bb.

Constraints
  • 1 ≤ len(text) ≤ 1000
  • text consists of digits and English letters
  • The longest palindromic piece is unique
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCounting Palindromic Pieces of a String MediumFalling Domino Row MediumWord Growth Chain MediumNearly Matching Picks MediumErasable Marked Letters Medium
loading Python…
Input
text="xlevelx9"
Expected
'xlevelx'

Run checks the first 3 tests in your browser. Submit checks all 103 tests on the server, hidden ones included.