Back to Practice Dashboard
Top 150 InterviewMedium

Palindromic Substrings

Learn how to solve the 'Palindromic Substrings' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Medium

Given a string s, return the number of palindromic substrings in it.

A string is a palindrome when it reads the same backward as forward. A substring is a contiguous sequence of characters within the string.

Write a function countSubstrings(s: str) -> int.

Constraints
  • 1 <= len(s) <= 1000
  • s consists of lowercase English letters

Examples

Example 1
Input
s = "abc"
Output
3
Explanation

Three palindromic substrings: "a", "b", "c".

Example 2
Input
s = "aaa"
Output
6
Explanation

Six palindromic substrings: "a", "a", "a", "aa", "aa", "aaa".

Need a Hint?
Define subproblem states, establish the recurrence relation, and use memoization (top-down) or tabulation (bottom-up).
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor