Try to find a word in the dictionary that has most letters given

I recently came across an algorithm design problem that I’d like some help with:

Given some letters, for example a,e,o,g,z,k,l,j,w,n and a dictionary of words. Find a word in the dictionary that has most letters.

My first attempt:

Let us assume that the dictionary is in a tree. Start by finding the permutations of the given letters, here we could be using recursion, we can prune the recursion tree, by checking the letters in the dictionary. and we maintain a variable which holds the largest string formed till now.

How is this solution? Is there a better one?

9

The best you can do is reduce the number of comparisons to the number of letters in the dictionary.

sought: a,e,o,g,z,k,l,j,w,n

  1. make index of alphabet where sought keys have value 1, the rest: 0.

       index={a:1, b:0, c:0, d:0, e:1, f:0, g:1...}
    
  2. Iterate over each word of dictionary. Add value of the index to sum of that word. Remember word position and value if it’s greater than best.

    max=0;
    max_index=0;
    
    foreach(dictionary as position=>word)
    {
        sum=0;
        foreach(word as letter)
        {
          sum += index[letter];
        }
        if(sum > max)
        {
            max = sum;
            max_index = position;
        }
    }
    

max_index points to the word with maximum of the letters.
Some optimizations may be skipping words shorter than the current max, or starting with dictionary sorted by word length and stopping once word length drops to max currently found.

This is assuming letters from the list are allowed to repeat any number of times. If they are not, make the index contain number of given type of letters, increment sum by 1 on each find of non-zero index value and decrement the index. (reset index on each line.)

In this time optimizations could be, on top of the previous ones: abort checking word if less than max-sum letters remain, abort operation if word with all letters is found.

1

I’d like to add to SF’s solution a bit. Not sure if I am correct in my analysis, but anyway:

If your preprocessing is free (considering search will be done enough times) you can preprocess each word in a dict by producing for it an entity like the index SF mentioned. So each word becomes a number like

01000100011101.... (up to the last letter a set of words can consist of)

where each 1 represents if the word has this letter or not (let’s skip the case where it has double or more for simplicity).

You can further arrange this transformed dict by the amount of different letters the word has, to start searching with ones that contain most letters and be able to cut off search early once you enter the range that can’t posibly have more matches than you already have.

When iterating over each word of a dictionary (now reduced to numbers) you simply XOR this number with the number produced from the set of the letters you are looking for and calculate its hamming weight(the number of 1s) which is essentially what you are looking for.

https://en.wikipedia.org/wiki/Hamming_weight

As the size of input is always constant (the size of the alphabet instead of variable word size) it can be done efficiently with publicly available algos. This way you wont have to compare letters one by one. Dealing with double/triple letters can be done by augmenting structures I suppose.

Just want to provide an alternative solution. If the number of random letters is small, you can try to generate all different permutations of the letters and check whether the dictionary contains a permutation.

For example, if you only have 3 letters “a”, “b” and “c”.

All the permutations are “abc”, “bca”, “cab”, “ab”, “bc”, “ac”, “a”, “b” and “c”. You can go from the longest ones to the shortest and check whether the dict contains the word.
It is more efficient when the dictionary is large and the number of random letters is small.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật