Skip to main content

Spelling Bee Word Finder

easy
Software engineer

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:

  1. It must have a length of at least 4 characters.
  2. It must contain the requiredLetter at least once.
  3. Every character in the word must be either the requiredLetter or one of the optionalLetters. 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 <= 100
  • requiredLetter is a single uppercase English letter.
  • optionalLetters contains distinct uppercase English letters.

Screening

ArrayEnumerationHash TableString
Language
Code editor loads in the browser.

Output

Input

"O"
["G","L","E","P","T","N"]
["ENCODE","NONCE","DONE","NODE","POLE","TONE","TOLL","GONE","TROPE","TON","TONER","PLOT","PNG","OPEN"]

Expected

["POLE","TONE","TOLL","GONE","PLOT","OPEN"]

Your output

Run to see your output.