10. Regular Expression Matching

Regular Expression Matching (LeetCode 10) is a Hard Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Dynamic Programming (DP) Patterns pattern, in the 2D Array (Edit Distance / Levenshtein Distance) subpattern.

How to solve Regular Expression Matching

Regular Expression Matching is a Edit Distance problem, part of Dynamic Programming (DP) Patterns. Calculate minimum edit operations to transform one string to another using 2D DP.

Work towards the solution in these steps:

  1. Consider whether a match can be described by the positions reached in both `s` and `p`.
  2. For a letter or `.`, advance in both strings only when the current characters are compatible.
  3. When the next pattern symbol is `*`, consider both using zero copies and consuming one matching character while staying on the same pattern element.

The reference solution runs in O(|s| * |p|) time and O(|s| * |p|) space.

The Regular Expression Matching solution and editorial walks through the full approach with reference code.

Problem statement

Implement isMatch to determine whether the entire string s is described by the pattern p. Return true only when every character in s is consumed by the pattern; otherwise return false.

The pattern contains lowercase letters, ., and . A lowercase letter matches itself, while . matches any single lowercase character. The symbol applies to the immediately preceding pattern element and allows that element to occur zero or more times.

Matching is performed against all of s, not merely a prefix. Every in p is preceded by a valid pattern element, so the pattern is always structurally valid.

Example 1:

Input: s = "candle", p = "c.nd." Output: true Explanation: The dot matches a, and . matches the remaining le, so the complete string is covered.

Example 2:

Input: s = "abz", p = "a.c" Output: false Explanation: Although . can match b, the final c cannot match the final character z.

Example 3:

Input: s = "", p = "q" Output: true Explanation: The pattern element q may occur zero times, which matches the empty string.

Constraints:

- 0 <= s.length <= 20 - 0 <= p.length <= 20 - s contains only lowercase English letters - p contains only lowercase English letters, '.' and '' - Every '' in p is preceded by a valid character or '.' - The match must consume all of s

Topics and companies

Topics: Dynamic Programming, String, String Matching.

How to practise Regular Expression Matching on Thita.ai

Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(|s| * |p|) time and O(|s| * |p|) space.

Open Regular Expression Matching in the code editor, or read the Regular Expression Matching editorial for a worked solution with its approach and complexity analysis.

Where Regular Expression Matching sits in the DSA pattern sheet

Problems related to Regular Expression Matching

Other problems that use the same 2D Array (Edit Distance / Levenshtein Distance) and Dynamic Programming (DP) Patterns techniques:

Regular Expression Matching FAQ

What is the LeetCode number of Regular Expression Matching?

Regular Expression Matching is LeetCode problem 10, rated Hard.

What pattern is used to solve Regular Expression Matching?

Regular Expression Matching is a Edit Distance problem. It belongs to the Dynamic Programming (DP) Patterns pattern, in the 2D Array (Edit Distance / Levenshtein Distance) subpattern.

What is the time complexity of Regular Expression Matching?

The reference solution runs in O(|s| * |p|) time and O(|s| * |p|) space.

Preparing this workspace.