MEDIUM NC#104 Blind #58 Dynamic Programming / Expand Around Center

647. Palindromic Substrings

📖 Problem

Given a string s, return the number of palindromic substrings in s. A palindrome string is a string that reads the same backward as forward.

🧠 Visual Learning Aid

1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases

JS/TS Refreshers

  • Array methods (`push`, `pop`, `shift`, `slice`)
  • Object/Map/Set usage patterns
  • Function parameter and return typing
  • Array DP table updates
  • State transition thinking
  • Base case initialization

Logical Thinking Concepts

  • Define invariants before coding
  • Check edge cases first (`[]`, single element, duplicates)
  • Estimate time/space before implementation
  • Apply Dynamic Programming reasoning pattern
  • Apply Recursion reasoning pattern

💡 Approach

  • Use expand around center technique: each character and gap can be a center
  • Expand from each center and count palindromes
  • Alternatively, use DP to build palindrome table
  • Time: O(n²), Space: O(1) for expand, O(n²) for DP

🛠️ Hints & Pitfalls

Hints

  • Use expand around center technique: each character and gap can be a center
  • Expand from each center and count palindromes
  • Alternatively, use DP to build palindrome table

Common Pitfalls

  • Time: O(n²), Space: O(1) for expand, O(n²) for DP

🧪 Test Cases

Hidden tests on submit: 3

Test Case 1
Not run
Input:
countSubstrings('abc');
Expected:
3
Test Case 2
Not run
Input:
countSubstrings('aaa');
Expected:
6
Test Case 3
Not run
Input:
countSubstrings('ababa');
Expected:
9

📝 Code Editor

📚 Reference Solution

⌘K Search ⌘↩ Run ⌘S Submit