Longest Palindromic Substring

viaGlassdoor

Problem: Given a string s, find the longest palindromic substring in s.

Constraints: Case-sensitive matching; if multiple longest palindromes exist, any one is acceptable.

Example: Input: "babad" -> Output: "bab" (or "aba"). Input: "cbbd" -> Output: "bb".

Approach: Expand-around-center: for each index (and each gap between two indices, to cover even-length palindromes), expand outward while the characters on both sides match, tracking the longest palindrome found. O(n^2) time, O(1) space. (Manacher's algorithm achieves O(n) time if required.)

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account