101 Logo
onenoughtone

Problem Statement

Palindromic Substring Finder

You are a software developer working on a text analysis tool for a publishing company. The company wants to identify palindromic patterns in manuscripts to study the author's writing style.

Given a string s, your task is to find the longest substring that is a palindrome. A palindrome is a string that reads the same forward and backward.

Unlike the subsequence problem, a substring is a contiguous sequence of characters within a string. For example, in the word "babad", the substrings "bab" and "aba" are palindromes, but "babad" is not.

Examples

Example 1:

Input: s = "babad"
Output: "bab"
Explanation: The longest palindromic substring is "bab" or "aba". Both have length 3, but we return the first one we find, which is "bab".

Example 2:

Input: s = "cbbd"
Output: "bb"
Explanation: The longest palindromic substring is "bb", which has a length of 2.

Example 3:

Input: s = "racecar"
Output: "racecar"
Explanation: The entire string "racecar" is a palindrome, so it is the longest palindromic substring.

Constraints

  • 1 <= s.length <= 1000
  • s consists only of lowercase English letters.

Problem Breakdown

To solve this problem, we need to:

  1. A palindrome reads the same forward and backward, so we need to find a contiguous substring with this property.
  2. We can check each possible center of a palindrome and expand outward to find the longest palindrome.
  3. There are two types of palindromes: odd-length (with a single character at the center) and even-length (with two identical characters at the center).
  4. Dynamic programming can also be used to solve this problem by building up solutions for smaller substrings.
ProblemSolutionCode
101 Logo
onenoughtone