Dashboard
#53. Palindromic SubstringsMedium1-D Dynamic Programming

Given a string s, return the number of substrings within s that are palindromes.

A palindrome is a string that reads the same forward and backward.

Example 1:

Input: s = "abc"

Output: 3

Explanation: "a", "b", "c".

Example 2:

Input: s = "aaa"

Output: 6

Explanation: "a", "a", "a", "aa", "aa", "aaa". Note that different substrings are counted as different palindromes even if the string contents are the same.

Constraints:

  • 1 <= s.length <= 1000
  • s consists of lowercase English letters.
Examples
Example 1:
Input: s = "abc"
Output: 3
Example 2:
Input: s = "aaa"
Output: 6
Constraints
  • `1 <= s.length <= 1000`
  • `s` consists of lowercase English letters.
Prerequisites
Target: O(N) time • O(1) spaceTwo Pointers / String / Dynamic Programming
Socratic AI Tutor
Gemini 3.8 Flash
Loading editor...

Click "Run" or press Ctrl+Enter to execute test cases.