Spelling Bee Word Finder
You are tasked with creating a "Spelling Bee" word finder. You are given a requiredLetter, a list of optionalLetters, and a dictionary of words. Your goal is to find all the words from the dictionary that meet a specific set of rules.
A word is considered valid if it satisfies all of the following conditions:
- It must have a length of at least 4 characters.
- It must contain the
requiredLetterat least once. - Every character in the word must be either the
requiredLetteror one of theoptionalLetters. No other characters are permitted.
You should return a list of all the valid words in any order.
Example 1
Input
requiredLetter = "O", optionalLetters = ["G","L","E","P","T","N"], dictionary = ["ENCODE", "NONCE", "DONE", "NODE", "POLE", "TONE", "TOLL", "GONE", "TROPE", "TON", "TONER", "PLOT", "PNG", "OPEN"]Output
["POLE", "TONE", "TOLL", "GONE", "PLOT", "OPEN"]Explanation
Let's check a few words: - 'PLOT': Length 4 (Valid), Contains 'O' (Valid), Letters {P, L, O, T} are all in the allowed set {O, G, L, E, P, T, N} (Valid). - 'ENCODE': Contains 'C' and 'D', which are not in the allowed set (Invalid). - 'TON': Length is 3, which is less than the required 4 (Invalid). - 'TONER': Contains 'R', which is not in the allowed set (Invalid).
Example 2
Input
requiredLetter = "A", optionalLetters = ["B", "C", "D", "E"], dictionary = ["ACE", "BADE", "CAB", "DEED", "FACE"]Output
["BADE"]
Example 3
Input
requiredLetter = "S", optionalLetters = ["T", "A", "R", "E"], dictionary = ["STARE", "STARS", "TEARS", "RARE", "STRESS", "TESTS", "SEAT", "TARTS", "STREET"]Output
["STARE", "STARS", "TEARS", "STRESS", "TESTS", "SEAT", "TARTS", "STREET"]
Constraints
1 <= dictionary.length <= 10⁵4 <= dictionary[i].length <= 100requiredLetteris a single uppercase English letter.optionalLetterscontains distinct uppercase English letters.
Spelling Bee Word Finder
Overview
The problem asks us to filter a dictionary based on three specific constraints reminiscent of the popular "Spelling Bee" word game. We need to identify words that are long enough (at least 4 characters), include a mandatory "center" letter, and are composed exclusively of a restricted set of allowed characters.
The core of the challenge lies in efficiently checking these character-based constraints for each word in a potentially large dictionary.
Brute Force
In a brute-force approach, we iterate through every word in the dictionary and verify the three rules sequentially. For the character set constraint, we can check every character of each word to see if it exists in the combined list of requiredLetter and optionalLetters.
def find_valid_words(requiredLetter: str, optionalLetters: list[str], dictionary: list[str]) -> list[str]:
valid_words = []
# Combine allowed letters into a single list/string for searching
allowed = [requiredLetter] + optionalLetters
for word in dictionary:
# Rule 1: Minimum length of 4
if len(word) < 4:
continue
# Rule 2: Must contain the required letter
if requiredLetter not in word:
continue
# Rule 3: All characters must be in the allowed set
is_valid = True
for char in word:
if char not in allowed:
is_valid = False
break
if is_valid:
valid_words.append(word)
return valid_words
Time complexity: O(N * L * M) — Where N is the number of words, L is the average length of a word, and M is the number of allowed letters (due to 'in' checks on a list). Space complexity: O(N * L) — To store and return the list of valid words.
Optimal / Best Approach
The bottleneck in the brute-force approach is checking if every character in a word exists in the allowed list repeatedly. We can optimize this by using a Set for the allowed characters, which provides $O(1)$ average-time lookups.
Furthermore, we can use Set Theory to simplify the logic: a word is valid (for rule 3) if the set of unique characters in the word is a subset of the set of allowed characters.
def find_valid_words(requiredLetter: str, optionalLetters: list[str], dictionary: list[str]) -> list[str]:
# Use a set for O(1) lookups and subset operations
allowed_set = set(optionalLetters)
allowed_set.add(requiredLetter)
result = []
for word in dictionary:
# Rule 1: Length check is O(1) in Python
if len(word) < 4:
continue
# Rule 2: Required letter check
if requiredLetter not in word:
continue
# Rule 3: Character composition check
# 'set(word) <= allowed_set' checks if all chars in word are in allowed_set
if set(word).issubset(allowed_set):
result.append(word)
return result
Time complexity: O(N * L) — We iterate through N words, and for each word, we perform operations proportional to its length L (creating a set and checking subset/containment). Space complexity: O(M + (N * L)) — We store M allowed characters in a set, plus the space required for the output list of valid words.
Why this works
- Set Lookups: Converting
optionalLettersandrequiredLetterinto a singlesettransforms a linear search $O(M)$ into a constant time $O(1)$ operation. - Subset Property: The condition that a word must only contain letters from the allowed list is mathematically equivalent to saying the set of characters in the word is a subset of the allowed characters.
- Short-Circuiting: By checking the length and the
requiredLetterfirst, we avoid the overhead of set creation for words that are already disqualified by simpler rules.
Fast
Comments on the solution
No comments on the solution yet. Start a thread about the editorial or your own approach.
No comments yet for Spelling Bee Word Finder. Be the first to start the thread.
No submissions recorded for this problem yet. Use Submit in the editor — each judge run is stored here.
Output
Input
Expected
Your output
Run to see your output.