AlgoMaster Logo

Wildcard Matching

Ashish

Ashish Pratap Singh

hard

Problem Description

Solve it on LeetCode

Approaches

1. Recursive Backtracking

Intuition:

The problem of wildcard matching can be thought of in a recursive manner by breaking down the problem into simpler sub-problems. If the current characters match (considering ? matches any single character), we can recursively match the rest of the string. If we encounter a *, we have the choice to ignore it or assume it matches one or more characters. This approach, however, is non-optimal due to potential overlapping sub-problems but provides a good base for understanding.

Code:

2. Memoization

Intuition:

Memoization optimizes the recursive approach by storing results of previously solved sub-problems, so they are not recomputed. This reduces the time complexity significantly by caching intermediate results.

Code:

3. Dynamic Programming

Intuition:

The dynamic programming solution uses a 2D table where dp[i][j] stores whether the first i characters of the string match the first j characters of the pattern. Use iteration to fill out the DP table based on matching rules and previously computed values. This ensures optimal substructure and overlapping sub-problems are effectively handled.

Code: