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 love to explain and answer questions on programming problems, the kind you find in coding interviews. I publish a new programming problem and its solution every month. Did I mention that I love to answer questions?