File Name Matching with Wildcards - aloalgo

File Name Matching with Wildcards

Hard

You are building a file search tool. You are given a list of file names and a search pattern that may include wildcards, return all file names that match the pattern exactly.

Wildcard Rules:

  • '?' matches any single character.
  • '*' matches any sequence of characters (including an empty sequence).

Your task is to implement a function that takes a list of file names and a pattern, and returns a list of all file names that precisely match the given pattern according to the wildcard rules.

Example 1

Inputs
files = ["readme.md", "eport.pdf", "resume.doc"]
pattern = "r*.?d"
Output
["readme.md"]
Explanation:

The pattern "r*.?d" matches "readme.md": 'r' matches 'r', '*' matches "eadme", '.' matches '.', '?' matches 'm', and 'd' matches 'd'.

Example 2

Inputs
files = ["abc.txt", "ab.txt", "a.txt", "x.txt", "a.csv"]
pattern = "a*.txt"
Output
["abc.txt", "ab.txt", "a.txt"]
Explanation:

The pattern "a*.txt" matches any file name starting with 'a', followed by any sequence of characters, and ending with ".txt".

Example 3

Inputs
files = ["photo.jpg", "document.pdf", "image.png", "archive"]
pattern = "*.*"
Output
["photo.jpg", "document.pdf", "image.png"]
Explanation:

The pattern "." matches any file name that contains at least one character followed by a dot and then any characters, effectively matching files with an extension. "archive" does not have a dot.

Loading...
Inputs
files = ["readme.md", "eport.pdf", "resume.doc"]
pattern = "r*.?d"
Output
["readme.md"]

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!