All problemsBack
Palindromic Substrings
mediumGiven 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.
Test Cases
Copy an input into themain harness and Run to verifyInput
s = "abc"Expected Output
3Explanation: Three palindromic strings: "a", "b", "c".
Input
s = "aaa"Expected Output
6Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
Constraints
- 1 <= s.length <= 1000
- s consists of lowercase English letters.
Hints
Hint 1 — click to reveal
Every palindrome has a centre — either a character, or the gap between two characters.
Hint 2 — click to reveal
There are 2n - 1 possible centres. Expand outward from each.
Java Compiler
Powered by OneCompiler. Starter code loads automatically — edit and hit Run.