DSA · Strings · #103
Longest Palindromic Substring
Module 62 · difficulty 3/5·⏱ 30:00starts on first keystroke
Given a string `s`, return the **longest palindromic substring** in `s`. A palindrome reads the same forwards and backwards. If several substrings share the maximum length, returning any one of them is acceptable, but the provided tests pin a single deterministic answer (the first such substring by starting index) — implement `longestPalindrome(s)` to return that one. Implement the function: ```js function longestPalindrome(s) { /* ... */ } ```
Examples
s = "babad"→"bab"— "aba" is also a valid palindrome of length 3; the first by start index is "bab".s = "cbbd"→"bb"— The longest palindrome has length 2.s = "a"→"a"— A single character is trivially a palindrome.
Constraints
- · 1 <= s.length <= 1000
- · s consists of only digits and English letters.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.