Palindromic Substrings - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: s = "abc"
Output: 3
💡 Note: Three palindromic substrings: "a", "b", "c". Each single character is a palindrome.
Example 2 — With Longer Palindromes
$ Input: s = "aaa"
Output: 6
💡 Note: Six palindromic substrings: "a", "a", "a", "aa", "aa", "aaa". Multiple overlapping palindromes exist.
Example 3 — Mixed Palindromes
$ Input: s = "aba"
Output: 4
💡 Note: Four palindromic substrings: "a" at index 0, "b" at index 1, "a" at index 2, and "aba" spanning the whole string.

Constraints

  • 1 ≤ s.length ≤ 1000
  • s consists of lowercase English letters

Visualization

Tap to expand
Count Palindromic SubstringsInput:abaAll Palindromic Substrings:"a" (0,0)"b" (1,1)"a" (2,2)"aba" (0,2)Output:4Count = 3 single characters + 1 full palindrome = 4
Understanding the Visualization
1
Input String
Given string with characters to analyze
2
Find Palindromes
Identify all substrings that read same forwards and backwards
3
Count Total
Return the total number of palindromic substrings
Key Takeaway
🎯 Key Insight: Every palindrome has a center - expand around each possible center to find all palindromic substrings efficiently
Asked in
Facebook 45 Google 38 Amazon 32 Microsoft 28
285.0K Views
High Frequency
~15 min Avg. Time
8.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen