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 . )
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.
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.)
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With