HARD NC#121 Dynamic Programming

10. Regular Expression Matching

📖 Problem

Given an input string s and a pattern p, implement regular expression matching with support for '.' and ''. The matching should cover the entire input string (not partial).

🧠 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

  • Use dp[i][j] = does s[0:i] match p[0:j+1]
  • Handle '.' (matches any char) and '*' (matches 0 or more of preceding)
  • Time: O(m * n), Space: O(m * n)

🛠️ Hints & Pitfalls

Hints

  • Use dp[i][j] = does s[0:i] match p[0:j+1]
  • Handle '.' (matches any char) and '*' (matches 0 or more of preceding)
  • Time: O(m * n), Space: O(m * n)

🧪 Test Cases

Hidden tests on submit: 1

Test Case 1
Not run
Input:
isMatch('aa', 'a');
Expected:
false
Test Case 2
Not run
Input:
isMatch('aa', 'a*');
Expected:
false
Test Case 3
Not run
Input:
isMatch('ab', '.*');
Expected:
false

📝 Code Editor

📚 Reference Solution

⌘K Search ⌘↩ Run ⌘S Submit