Longest Word
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 kikj
.
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.
Input.
The input file consists of one or more cases. Each case consists of a
number n
on a single line followed by n
lines, each consisting of
a word. Each word consists of lowercase ascii letters from a
to
z
. The input is terminated by EOF. The following is a sample input
file.
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.
Solution
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 i
, one j
, and two k
, thus giving a
grand total of 4 letters.