#647

Palindromic Substrings

Medium Expand around centre

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Count how many substrings (by position) are palindromes.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: how many substrings (by position) are palindromes?

Why this works: expand around every centre; each successful step is one more palindrome.

Example: "aaa"

single letters: 3"aa" twice: 2"aaa": 1 → 6

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Generating every substring and checking it — O(n³).

Solve on LeetCode ↗
Complexity
  • Time: O(n²).
  • Space: O(1).
Topic: Strings
🎬 Episodes 16–20: video coming soon