Longest mirror inside a word
Reported in interviews at (where)
A palindrome reads the same backwards as forwards. Given a string s, return its longest piece (characters next to each other) that is a palindrome.
If several pieces share the longest length, return the one that starts first.
- Input
s = "bananas"- Output
"anana"
"anana" reads the same both ways; nothing longer does.
- Input
s = "abcd"- Output
"a"
Every single letter is a palindrome; "a" starts first.
- Input
s = "xyyx7"- Output
"xyyx"
An even-length mirror.
Limits
1 <= s.length <= 2000s holds lowercase English letters and digits.
Hints
Hint 1
Every palindrome has a centre: a letter, or the gap between two letters.
Hint 2
Grow outwards from each of the 2n - 1 centres.
Where this was reported
- IBM, Software developer on-campus interview, 2021 (GeeksforGeeks)
- IBM, ISDL software developer on-campus interview, 2022 (GeeksforGeeks)
Our own problem on the task these candidates describe; the wording, examples and tests are InternDoor’s.
Run your code to check it on the examples. Submit runs every hidden test.
