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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code