Uber · Question 75 · Dynamic Programming
Regular Expression Matching
Problem
Implement regular expression matching with support for '.' (any char) and '*' (zero or more of the previous char).
Approach
DP[i][j] = whether s[:i] matches p[:j]. A '*' either skips its char or consumes one more match; otherwise chars must match.
Time O(m×n)Space O(m×n)
How it works
- 1Empty pattern matches only the empty string (plus a* cases).
- 2'*' means skip its char or consume one matching char.
- 3Other positions need a direct or '.' match.
Sample input / output
Input: aa a* Output: True
Constraints
1 <= s, p lengths <= 20; letters, '.' and valid '*' only.
Solution — read it, then walk it
Time O(m×n)Space O(m×n)Loops 3Branches 7Returns 4source · Python
Scroll sideways for wide charts — the view follows the active step.
- start / end
- statement
- condition
- loop
- returns
- definition
1/30
StartSTART
Interactive control-flow diagram and step-by-step code walkthrough.
Recommended resourcesAffiliate links · may earn us a commission
Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits