36 · DP Patterns

#647 Palindromic Substrings

Mediumsolve on LeetCode ↗

frequently reported atMetaWalmartAmazonMicrosoftGoogle

the problem

In plain words

Count the substrings of s that read the same forwards and backwards. A substring is a contiguous block of letters, and blocks at different positions count separately even when they spell the same thing.

in s = "abc"

out 3

Only the single letters.

in s = "aaa"

out 6

Three "a", two "aa", one "aaa".

  • 1 ≤ s.length ≤ 1000
  • Lowercase English letters

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #5
      Longest Palindromic Substring
      Mediumin Dynamic Programming
    2. #131
      Palindrome Partitioning
      Mediumin Backtracking
    3. #125
      Valid Palindrome
      Easyin Two Pointers