
Problem
Solution
Submissions
Longest Palindromic Substring
Certification: Intermediate Level
Accuracy: 0%
Submissions: 0
Points: 10
Write a JavaScript program to find the longest palindromic substring in a given string. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward. For example, "racecar" is a palindrome because it reads the same in both directions.
Example 1
- Input: s = "babad"
- Output: "bab"
- Explanation:
- The string "babad" contains multiple palindromic substrings.
- We can find "bab" starting at index 0, which has length 3.
- We can also find "aba" starting at index 1, which also has length 3.
- Both are valid answers since they have the same maximum length.
- The string "babad" contains multiple palindromic substrings.
Example 2
- Input: s = "cbbd"
- Output: "bb"
- Explanation:
- The string "cbbd" is analyzed for palindromic substrings.
- Individual characters 'c', 'b', 'b', 'd' are palindromes of length 1.
- The substring "bb" at indices 1-2 is a palindrome of length 2.
- "bb" is the longest palindromic substring in this case.
- The string "cbbd" is analyzed for palindromic substrings.
Constraints
- 1 ≤ s.length ≤ 1000
- s consists of only lowercase English letters
- Time Complexity: O(n^2)
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use the "expand around centers" approach to check for palindromes
- For each character, treat it as a potential center of a palindrome
- Expand outward from each center while characters match
- Handle both odd-length and even-length palindromes separately
- Keep track of the longest palindrome found so far