Given a set of strings, find the length of any longest word you can make with the letters that are common.
For example, consider the following set of strings.
The length of a longest word is 4 because a longest word is
Edit. This post was previously titled ‘Longest Common Anagram’ and had a different statement. Thanks to Marcelo Melendez for pointing out that the title and statement were confusing.
The input file consists of one or more cases. Each case consists of a
n on a single line followed by
n lines, each consisting of
a word. Each word consists of lowercase ascii letters from
z. The input is terminated by EOF. The following is a sample input
Output. The output file consists of one line per case, each consisting of a single integer that indicates the length of any longest word for the case. The following is the output file that corresponds to the sample input file.
Consider the count of letters for each string.
A longest word consists only of letters appearing in the words. For each letter, a longest word has as many copies of the letter as the minimum count amongst the strings. Consider the minimum count of letters amongst the strings.
Given the minimum count of letters, we know that a longest word
consists of one
j, and two
k, thus giving a
grand total of 4 letters.
Want to read more?
I regularly write solutions to programming problems that you may or may not find in technical job interviews. I also love to explain those solutions and answer questions. If you would like to get the latest problem + solution, subscribe to the newsletter or subscribe via RSS. You can also follow me on Twitter, GitHub, and LinkedIn.