00:00

Problem Statement

Input/Output Examples

Visual Explanation

For a star, a cell is true if the pair x* is skipped (two columns to the left) or if the current character matches x and the row above is already true in the same column, meaning the star takes one more character.

Approaches

Both approaches use the same two rules for x*: skip it, or use it for one more character. One fills a table; the other recurses with a cache.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n is the length of the string and m the length of the pattern. Each of the (n + 1) × (m + 1) states is computed once.

Interview Tips and Recommended Approach

  • Treat x* as one unit. The star never stands alone; always look ahead one character in the pattern.
  • Initialise the empty-string row. Patterns like a*b*c* match "".
  • Compare with wildcard matching. There, * matches any sequence on its own; here it repeats the previous element.
  • Start with plain recursion, then add the cache. It shows how the exponential solution becomes polynomial.

Recommended Approach: Bottom-Up DP Table

Why: it is iterative, covers every case explicitly and is easy to test row by row. Many people derive the recursive version first and then convert it to this table.