Match a Whole String with Wildcards

Quick Overview

Check complete-string wildcard matching with question marks and stars, preserving empty-string cases and zero-length star matches.

Match a Whole String with Wildcards

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem Return whether the pattern `mask` matches the whole string `text` under these wildcard rules: - A lowercase letter in `mask` matches the same letter in `text`. - `?` matches exactly one character. - `*` matches a sequence of any length, including zero characters. Every pattern character participates in the match, and the match must consume all of `text`. Finding a matching portion of the text is insufficient. ### Examples **Example 1** ```text Input: text = "cabin", mask = "c*?n" Output: true ``` The star can cover `"ab"`, the question mark covers `"i"`, and the literal letters match the ends of the text. **Example 2** ```text Input: text = "", mask = "*?" Output: false ``` The star may cover no characters, but the question mark still needs one character, which the empty text cannot supply. ### Constraints - `0 <= text.length <= 2000` - `0 <= mask.length <= 2000` - The text contains lowercase English letters only. - The mask contains lowercase English letters, `?`, and `*` only.

Overview: Check complete-string wildcard matching with question marks and stars, preserving empty-string cases and zero-length star matches.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return whether the pattern mask matches the whole string text under these wildcard rules:

  • A lowercase letter in mask matches the same letter in text .
  • ? matches exactly one character.
  • * matches a sequence of any length, including zero characters.

Every pattern character participates in the match, and the match must consume all of text. Finding a matching portion of the text is insufficient.

Examples

Example 1

Input: text = "cabin", mask = "c*?n"
Output: true

The star can cover "ab", the question mark covers "i", and the literal letters match the ends of the text.

Example 2

Input: text = "", mask = "*?"
Output: false

The star may cover no characters, but the question mark still needs one character, which the empty text cannot supply.

Constraints

  • 0 <= text.length <= 2000
  • 0 <= mask.length <= 2000
  • The text contains lowercase English letters only.
  • The mask contains lowercase English letters, ? , and * only.