Longest Palindromic Substring
Part of Practice: Strings & Coins
Given a string s, return the longest contiguous substring of s that is a palindrome — a string that reads the same forwards and backwards, like "level" or "noon". If several palindromic substrings tie for longest, returning any one of them is accepted.
Constraints: 1 <= s.length <= 1000, and s contains only letters and digits. A single character is always a palindrome, so an answer always exists.
Examples
- Input:
- s = "babad"
- Output:
- "bab"
"aba" is an equally valid answer — both have length 3.
- Input:
- s = "cbbd"
- Output:
- "bb"
The longest palindrome here has even length; its center falls between the two b's.
- Input:
- s = "a"
- Output:
- "a"
A single character is a palindrome of length 1.
Hints
One at a time — struggle first, peek second.
Your thinking space — verify on LeetCode, then mark it.