Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Wildcard String Matching
00:00
5 left

Wildcard String Matching

HardPython

Problem

Rokt placement identifiers may use a compact pattern format. Given a pattern s1 and a candidate string s2, determine whether the pattern matches the entire string.

The pattern supports two wildcard forms:

  1. . matches exactly one arbitrary character.
  2. A digit d matches exactly d arbitrary characters. Each digit is an independent token, so 23 means two wildcards followed by three wildcards, not twenty-three.
  3. Any lowercase letter matches itself exactly.

Return True only when the complete pattern matches the complete candidate string. Return False otherwise.

Formal Specification

  • Input: two strings s1 and s2 containing lowercase letters, digits, and . in s1, and lowercase letters in s2.
  • Output: a boolean indicating whether s1 matches all of s2.
  • Matching is case-sensitive, and wildcard characters may match any character, including characters that would otherwise be literals.

Examples

Example 1

Input: s1 = "r.kt", s2 = "rokt"
Output: True

The . consumes o, leaving r, k, and t to match exactly.

Example 2

Input: s1 = "a3d", s2 = "axyzd"
Output: True

The digit 3 consumes xyz.

Example 3

Input: s1 = "r2k", s2 = "rokt"
Output: False

After r2 consumes rok, the remaining character is t, not k.

Constraints

  • 0 <= len(s1) <= 100,000
  • 0 <= len(s2) <= 1,000,000
  • s1 contains only lowercase letters, digits, and .
  • s2 contains only lowercase English letters
  • Each digit represents an independent wildcard count from 0 through 9

Function Signature

def matches_pattern(s1, s2):
Interviewer

Your question is Wildcard String Matching. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.