Spelling Bee
InterviewTime limit3sMemory limit1024 MB
Given a 7-letter Spelling Bee hexagon and a word list, the task is to print every word of length at least 4 that uses only those letters and contains the center letter.
- Level
Easy3 of 10
- Topics
- String, Implementation
- Solved
- No attempts yet
Problem

The New York Times publishes a daily puzzle called "Spelling Bee." The puzzle shows 7 letters in a hexagon: 6 letters around a center letter. The goal is to find as many words as possible that meet all of these conditions:
- They contain only letters shown in the hexagon.
- They are at least 4 letters long.
- They contain the center letter.
A letter may be used more than once, and not all letters need to be used.
After playing for a while, you get stuck. Then you remember that the Linux distribution on your computer includes a machine-readable file, /usr/share/dict/words, with 102,305 dictionary words.
You cannot excel at Spelling Bee, but you can excel at programming. So you decide to write a program that finds all solutions to a Spelling Bee puzzle in the dictionary.
Input
The input is a single test case. The first line has 7 distinct lowercase English letters. The first letter is the center letter. The next line contains an integer (), the size of the dictionary. This is followed by lines, each containing a dictionary word of lowercase English letters ().
Output
Print the words that match the Spelling Bee puzzle, in the order they appear in the dictionary, one per line. The input guarantees that at least one dictionary entry matches.