Meta AI receives candidate words for autocomplete suggestions. Given a list of lowercase words and a wildcard pattern, return every word that matches the entire pattern, preserving the original order and duplicates.
The pattern contains lowercase letters, ?, and *. A ? matches exactly one lowercase letter. A * matches zero or more lowercase letters. Matching must cover the complete word, not just a substring.
Implement filter_words(words, pattern):
words, a list of lowercase strings, and pattern, a lowercase string containing letters, ?, and *.pattern, in the same order as words.Example 1:
Input: words = ["chat", "chart", "chatter", "coat"], pattern = "ch*t"
Output: ["chat", "chart", "chatter"]
* absorbs zero characters in chat, one character in chart, and several characters in chatter. coat does not begin with ch.
Example 2:
Input: words = ["cat", "cut", "coat", "cart"], pattern = "c?t"
Output: ["cat", "cut"]
The ? matches exactly one character, so coat and cart are too long.
def filter_words(words, pattern):