Spelling Bee Word Filter
Problem statement
You are given an array of unique lowercase words dictionary, a one-character string required, and a six-character string optional.
Return every dictionary word that satisfies all of these rules:
- The word contains at least four letters.
- The required letter appears at least once.
- Every character is either the required letter or one of the optional letters.
- Allowed letters may be repeated any number of times.
Preserve the words' original dictionary order.
Function
spellingBeeWords(dictionary: String[], required: String, optional: String) → String[]Examples
Example 1
dictionary = ["encode","nonce","done","node","oven","deed","coded","envy","no"]required = "n"optional = "vcodey"return = ["encode","nonce","done","node","oven","envy"]deed and coded omit the required letter, while no is too short.
Example 2
dictionary = ["pole","tone","toll","gone","trope","ton","google","people","noon","longer"]required = "o"optional = "gleptn"return = ["pole","tone","toll","gone","google","people","noon"]ton is shorter than four letters. trope and longer contain the disallowed letter r.
Example 3
dictionary = []required = "a"optional = "bcdefg"return = []An empty dictionary has no matching words.
Constraints
0 <= dictionary.length <= 100000.- Every dictionary word is unique, nonempty, and contains only lowercase English letters.
- The total number of characters across
dictionaryis at most1000000. requiredcontains exactly one lowercase English letter.optionalcontains exactly six distinct lowercase English letters, none equal torequired.