Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortest word in a dictionary containing all letters

Tags:

algorithm

I was asked this question in an interview.

Given an array of characters, find the shortest word in a dictionary that contains all the characters. Also, propose an implementation for the dictionary that would optimize this function call.

for e.g. char[] chars = { 'R' , 'C' }. The result should be the word "CAR".

I could not come up with anything that would run reasonably quickly. I thought of pre-processing the dictionary by building a hash table to retrieve all words of a particular length. Then I could only think of retrieving all words in the increasing order of length and checking if the required characters were present in any of those ( maybe by using a bitmask . )

like image 384
frodo Avatar asked Feb 06 '15 22:02

frodo


People also ask

What is the shortest word in the dictionary?

The shortest word is a. Some might wonder about the word I since it consists of one letter, too. In sound, a is shorter because it is a monophthong (consists of one vowel), while I is a diphthong. Both do consist of one letter in the English writing system, and in most fonts I is the narrowest letter.

What is the shortest word with all 5 vowels?

Eunoia, at six letters long, is the shortest word in the English language that contains all five main vowels. Seven letter words with this property include adoulie, douleia, eucosia, eulogia, eunomia, eutopia, miaoued, moineau, sequoia, and suoidea. (The scientific name iouea is a genus of Cretaceous fossil sponges.)

What is the shortest English word ever?

One-letter words The indefinite article a is only capitalized when it begins a sentence, but the pronoun I is always capitalized. Another single-letter word that is always capitalized is O. It's not common in everyday writing, but it appears as an interjection in poetry.


2 Answers

This is a common software interview question, and its solution is this: sort the dictionary itself by length and sort each value alphabetically. When given the characters, sort them and find the needed letters.

like image 153
user3105700 Avatar answered Oct 06 '22 00:10

user3105700


First sort the dictionary in ascending order of length.

For each letter, construct a bit map of the locations in the dictionary of the words containing that letter. Each bit map will be long, but there will not be many.

For each search, take the intersection of the bitmaps for the letters in the array. The first one bit in the result will be at the index corresponding to the location in the dictionary of the shortest word containing all the letters.

like image 37
Patricia Shanahan Avatar answered Oct 06 '22 01:10

Patricia Shanahan