Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

10. Regular Expression Matching

Hard28.6% Acceptance
StringDynamic ProgrammingRecursion
Asked by:
Facebook
ProblemSolutions (12)VideosCompanies (9)Notes

Problem Statement

Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where:

  • '.' Matches any single character.​​​​
  • '*' Matches zero or more of the preceding element.

The matching should cover the entire input string (not partial).

Example 1:

Input: s = "aa", p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".

Example 2:

Input: s = "aa", p = "a*"
Output: true
Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".

Example 3:

Input: s = "ab", p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".

Constraints:

  • 1 <= s.length <= 20
  • 1 <= p.length <= 20
  • s contains only lowercase English letters.
  • p contains only lowercase English letters, '.', and '*'.
  • It is guaranteed for each appearance of the character '*', there will be a previous valid character to match.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
F
M
Microsoft
G
Google
A
Amazon
N
Nvidia
+4

Approach

Regular Expression Matching asks whether a string s matches a pattern p containing special characters . (matches any single character) and * (matches zero or more of the preceding element). A brute force recursive solution tries all possible interpretations of *, but this leads to heavy recomputation and exponential time.

A more efficient strategy uses Dynamic Programming. Define a DP state where dp[i][j] indicates whether the first i characters of s match the first j characters of p. The transitions depend on whether the current pattern character is a normal character, ., or *. When encountering *, you can either ignore the preceding element (zero occurrences) or consume characters from the string if they match.

This problem can be solved using either top-down recursion with memoization or bottom-up DP. Both approaches systematically avoid repeated work and correctly handle the pattern rules. The typical time complexity is O(m × n), where m and n are the lengths of the string and pattern.

Complexity

ApproachTime ComplexitySpace Complexity
Recursion with MemoizationO(m × n)O(m × n)
Bottom-Up Dynamic ProgrammingO(m × n)O(m × n)

Video Solution Available

Tushar Roy - Coding Made Simple

View all video solutions

Solutions (12)

Recursive Approach with Memoization

This approach uses recursion with memoization to solve the problem efficiently. We recursively compare characters of the input string and the pattern, addressing special characters like '.' and '*'. The memoization is used to store results of sub-problems to avoid redundant calculations, which drastically improves the performance.

Time Complexity: O(m * n), where m and n are the lengths of the string and pattern, respectively. We solve every subproblem once and store the result.
Space Complexity: O(m * n) due to recursion stack and memoization storage.

PythonJavaCC++C#JavaScript
1def isMatch(s, p):
2    memo = {}
3    def dfs(i, j):
4        if (i, j) in

Explanation

The Python solution uses a depth-first search (DFS) approach with memoization to tackle overlapping subproblems. We check if characters at position i in the string and j in the pattern match directly or through special symbols. The '*' in the pattern is handled by either skipping it or considering it as multiple matches.

Dynamic Programming Tabulation

This approach involves using a DP table to solve the problem by filling up a boolean matrix iteratively. This avoids recursion and stacks overhead, improving performance in certain scenarios. Each cell in the table represents whether the substring of the pattern matches the substring of the input.

Time Complexity: O(m * n) since we traverse the complete matrix.
Space Complexity: O(m * n) due to the DP table used to store subproblem solutions.

PythonJavaCC++C#JavaScript
1

Video Solutions

Watch expert explanations and walkthroughs

Regular Expression Dynamic Programming

Tushar Roy - Coding Made Simple
18:34263,593 views

Asked By Companies

9 companies
F
Facebook
M
Microsoft
G
Google
A
Amazon
N
Nvidia
B
Bloomberg
A
Adobe
A
Apple
U
Uber

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Longest Palindromic SubstringMedium
Generate ParenthesesMedium
Longest Valid ParenthesesHard
Wildcard MatchingHard
More similar problems

Related Topics

StringDynamic ProgrammingRecursion

Problem Stats

Acceptance Rate28.6%
DifficultyHard
Companies9

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Regular Expression Matching asked in FAANG interviews?

Yes, Regular Expression Matching is a well-known hard problem frequently discussed in FAANG-level interviews. It tests understanding of recursion, dynamic programming, and careful handling of pattern-matching rules.

What is the optimal approach for Regular Expression Matching?

The optimal approach uses dynamic programming. By defining a DP state that represents whether prefixes of the string and pattern match, you can systematically handle '.' and '*' rules while avoiding repeated computations.

Why is dynamic programming used in Regular Expression Matching?

Dynamic programming is used because many subproblems repeat when exploring different interpretations of '*'. Storing intermediate results ensures each state is computed only once, improving performance significantly.

What data structure is commonly used to solve Regular Expression Matching?

A 2D DP table or memoization map is commonly used. It stores whether a substring of the input string matches a substring of the pattern, enabling efficient state transitions.

Previous Problem

String to Integer (atoi)

Next Problem

Integer to Roman

memo
:
5
return
memo
[
(
i
,
j
)
]
6
if
j
==
len
(
p
)
:
7
return
i
==
len
(
s
)
8
match
=
i
<
len
(
s
)
and
(
s
[
i
]
==
p
[
j
]
or
p
[
j
]
==
'.'
)
9
if
(
j
+
1
)
<
len
(
p
)
and
p
[
j
+
1
]
==
'*'
:
10
memo
[
(
i
,
j
)
]
=
dfs
(
i
,
j
+
2
)
or
(
match
and
dfs
(
i
+
1
,
j
)
)
11
return
memo
[
(
i
,
j
)
]
12
if
match
:
13
memo
[
(
i
,
j
)
]
=
dfs
(
i
+
1
,
j
+
1
)
14
return
memo
[
(
i
,
j
)
]
15
memo
[
(
i
,
j
)
]
=
False
16
return
False
17
return
dfs
(
0
,
0
)
def
isMatch
(
s
,
p
)
:
2
dp
=
[
[
False
]
*
(
len
(
p
)
+
1
)
for
_
in
range
(
len
(
s
)
+
1
)
]
3
dp
[
0
]
[
0
]
=
True
4
for
j
in
range
(
1
,
len
(
p
)
)
:
5
if
p
[
j
]
==
'*'
:
6
dp
[
0
]
[
j
+
1
]
=
dp
[
0
]
[
j
-
1
]
7
for
i
in
range
(
len
(
s
)
)
:
8
for
j
in
range
(
len
(
p
)
)
:
9
if
p
[
j
]
==
'.'
or
s
[
i
]
==
p
[
j
]
:
10
dp
[
i
+
1
]
[
j
+
1
]
=
dp
[
i
]
[
j
]
11
elif
p
[
j
]
==
'*'
:
12
dp
[
i
+
1
]
[
j
+
1
]
=
dp
[
i
+
1
]
[
j
-
1
]
or
(
dp
[
i
]
[
j
+
1
]
if
p
[
j
-
1
]
==
s
[
i
]
or
p
[
j
-
1
]
==
'.'
else
False
)
13
return
dp
[
-
1
]
[
-
1
]

Explanation

The Python DP solution constructs a 2D boolean array where dp[i][j] signifies if the first i characters of the string match the first j characters of the pattern. The matrix is initialized with the base conditions and filled based on the characters in pattern string, iteratively updating for mismatches dictated by '*' or '.'.