MEDIUM NC#103 Blind #57 1-D Dynamic Programming / Expand Around Center

5. Longest Palindromic Substring

šŸ“– Problem

Given a string s, return the length of the longest palindromic substring in s.

🧠 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

šŸ’” Approach

  • → Expand around each character as center for potential palindrome
  • → Use helper function to check if substring is palindrome
  • → Track max length found
  • → Time: O(n²), Space: O(1)

šŸ› ļø Hints & Pitfalls

Hints

  • •Expand around each character as center for potential palindrome
  • •Use helper function to check if substring is palindrome
  • •Track max length found

Common Pitfalls

  • •Time: O(n²), Space: O(1)

🧪 Test Cases

Hidden tests on submit: 1

Test Case 1
Not run
Input:
longestPalindrome('babad');
Expected:
3
Test Case 2
Not run
Input:
longestPalindrome('cbbd');
Expected:
2
Test Case 3
Not run
Input:
longestPalindrome('a');
Expected:
1

šŸ“ Code Editor

šŸ“š Reference Solution

ā–¼
⌘K Search āŒ˜ā†© Run ⌘S Submit