Wildcard Matching

Zahid Un Nabi
4 min readMay 2, 2024

--

wildcard matching

Imagine you have a box of letter tiles, and each tile has one letter from the alphabet on it. Now, suppose you also have a magic tile: a star tile (*) that can be any word or even a space, and a question mark tile (?) that can be any letter.

Your friend gives you a secret code made of letters, stars, and question marks, and your challenge is to see if you can arrange your letter tiles to match this secret code. The star tile is very special because it can turn into a long word or disappear and become nothing! The question mark is a bit like a wild card in games; it can be any letter you need it to be, just for one spot though.

To solve this, you’d start by laying out your tiles to see if they can fit the pattern your friend gave you. You check each tile one by one:

  • If your tile matches the letter in the code, you move to the next one.
  • If you see a question mark in the code, you can put any letter there.
  • If there’s a star, you can skip some tiles or use none and see if it still works out.

You keep trying different ways to arrange your tiles until you find a way that matches the entire code, or you realize it’s impossible. This is like a puzzle where you figure out where each piece goes, and sometimes you have to try different options to see what fits best.

Let’s imagine you’re trying to solve a jigsaw puzzle, but this puzzle is a bit special — it’s more like a game of matching patterns. You have a string of characters (let’s call it the “string”), and a pattern that can include some wild cards like ? and *.

Here’s how you might go about solving this puzzle:

Start from the Beginning: Think of the pattern as a series of instructions on how to build a sentence using the letters from your string. You start at the beginning of both the string and the pattern.

Match One by One:

  • If the current character in the pattern is a letter (say a), check if the current character in the string is also that same letter (a). If yes, both the string and pattern move to the next character.
  • If the pattern has a ?, it’s like saying, “Any letter can fit here.” So whatever the letter is in the string, it’s considered a match, and you move both forward.

The Star * — The Game Changer: If you encounter a * in the pattern, it’s like a magic wand — it can become anything:

  • No letters at all.
  • A single letter.
  • A whole bunch of consecutive letters. Your job is to try all possibilities: skip no letters, skip one letter, skip two letters, and so on, until you find a way that allows the rest of the pattern to match the rest of the string.

Continue Until the End: Keep applying these rules as you move through the string and the pattern. If you reach the end of the pattern and every part of the string has been matched according to these rules, then you’ve solved the puzzle!

Check All Paths: Because of the *, you might need to try different paths to find the right fit. Sometimes you need to backtrack, meaning if one path doesn’t work out (say, skipping three letters leads to a dead end), you might go back and try skipping two letters instead.

Finish Line: You successfully complete the puzzle if you can find a path through the string that matches every instruction in the pattern. If there’s no such path, then the pattern doesn’t match the string.

It’s a bit like navigating through a maze where some signs (? and *) give you special powers to transform or teleport, making it an exciting and challenging adventure. Each decision affects the next step, requiring both strategy and flexibility to successfully match the entire string to the pattern.

class Solution:
def isMatch(self, s: str, p: str) -> bool:
slen, plen = len(s), len(p)
dp = [[False] * (plen + 1) for _ in range(slen + 1)]
dp[0][0] = True

for j in range(1, plen + 1):
if p[j - 1] == '*':
dp[0][j] = dp[0][j - 1]

for i in range(1, slen + 1):
for j in range(1, plen + 1):
if p[j - 1] == '*':
dp[i][j] = dp[i - 1][j] or dp[i][j - 1]
elif p[j - 1] == '?' or s[i - 1] == p[j - 1]:
dp[i][j] = dp[i - 1][j - 1]

return dp[slen][plen]

# Example usage:
sol = Solution()
print(sol.isMatch("aa", "a")) # Output: False
print(sol.isMatch("aa", "*")) # Output: True
print(sol.isMatch("cb", "?a")) # Output: False

In this intriguing puzzle of pattern matching with wildcards, our journey through the string and the pattern is akin to navigating a treasure map where? marks spots where any single treasure (character) fits perfectly, and * unfolds a magical bridge that can stretch over any length of the string, be it vast or just a tiny step.

As you explore various paths that these wildcards reveal, remember, that it’s not just about reaching the end but about finding a path that aligns perfectly with the rules laid out by the pattern. This adventure tests your strategic thinking and adaptability, much like a game where each move opens up new possibilities or challenges.

Successfully completing this match is satisfying, as it confirms you’ve masterfully followed the map to uncover how every piece of the string can fit under the guiding stars of the pattern. Whether you are a seasoned explorer of puzzles or just starting, this challenge offers a fun and educational way to enhance your problem-solving skills, proving that even the most complex problems have a solution waiting to be discovered if only you persist and explore every potential path.

--

--

Zahid Un Nabi
Zahid Un Nabi

Written by Zahid Un Nabi

Blockchain, Data Science & Big Data Analysis Enthusiastic | Python & Server Nerd