medium1-D Dynamic ProgrammingAmazonMicrosoftFacebook
Longest Mirror Substring
A text editor highlights the longest contiguous slice of a string that reads identical forwards and backwards.
Given a string s, return the longest palindromic substring in s.
Choose approach
b
0a
1b
2a
3d
4ilo
1/5
Narrationstep 1 of 5
Expand center 0; best = "b" (len 1).
Variables
center=0
best=b
len=1
How to think about it
- 2n − 1 candidate centers.
- Each expansion is O(n) worst-case.
Edge cases to catch in interview
- All equal chars -> whole string.
- No palindrome > 1 -> first char.
Examples
Example 1
"bb"{"s":"cbbd"}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed