Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Regular Expression Matching

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "10" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

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.

Explanation

Here's the approach to solve this regular expression matching problem:

  • Dynamic Programming: Use dynamic programming to build a table dp where dp[i][j] stores whether the first i characters of the string s match the first j characters of the pattern p.
  • Base Cases: Initialize the first row and column of the dp table appropriately to handle empty string and pattern cases.
  • Iteration: Iterate through the dp table, filling each cell based on whether the current characters in s and p match, and handling the special cases of '.' and '*'.

  • Runtime Complexity: O(m*n) where m is the length of string s and n is the length of the pattern p. Storage Complexity: O(m*n).

Code

    def isMatch(s: str, p: str) -> bool:
    """
    Implements regular expression matching with support for '.' and '*'.
    """
    m, n = len(s), len(p)
    dp = [[False] * (n + 1) for _ in range(m + 1)]

    # Base case: empty string and empty pattern match
    dp[0][0] = True

    # Handle patterns like a*, a*b*, a*b*c*
    for j in range(1, n + 1):
        if p[j - 1] == '*':
            dp[0][j] = dp[0][j - 2]

    # Fill the dp table
    for i in range(1, m + 1):
        for j in range(1, n + 1):
            if p[j - 1] == '.' or p[j - 1] == s[i - 1]:
                dp[i][j] = dp[i - 1][j - 1]
            elif p[j - 1] == '*':
                dp[i][j] = dp[i][j - 2]  # Zero occurrences of preceding element
                if p[j - 2] == '.' or p[j - 2] == s[i - 1]:
                    dp[i][j] = dp[i][j] or dp[i - 1][j]  # One or more occurrences
            else:
                dp[i][j] = False

    return dp[m][n]

More from this blog

C

Chatmagic blog

2894 posts