#B3773. Minimum Length Subsequence Covering All Alphabets

    ID: 11432 Type: Default 1000ms 256MiB

Minimum Length Subsequence Covering All Alphabets

Minimum Length Subsequence Covering All Alphabets

You may have seen the following famous sentence:

The quick brown fox jumps over the lazy dog.

This short sentence contains all $\displaystyle 26$ letters of the English alphabet and is widely used to display font effects. Even shorter is:

The five boxing wizards jump quickly.

Now, you are curious: Are there any other contiguous segments (subsequences) of words that contain all $\displaystyle 26$ letters at least once? From a long sequence of words crawled from the Internet, your task is to extract a contiguous sequence of words which satisfies the following conditions:

  • It contains every one of the $\displaystyle 26$ letters ('a' to 'z') at least once. (Letters are case-insensitive and you can assume the input words are in lowercase.)
  • The total number of letters in the segment is as small as possible.

If no such subsequence exists, output 0.

inputFormat

The input consists of two lines:

  1. The first line contains an integer n ($1 \le n \le 10^5$) representing the number of words.
  2. The second line contains n words separated by spaces. Each word is composed of lowercase English letters only.

outputFormat

Output a single integer: the minimum total number of letters in a contiguous subsequence of words that contains all 26 letters at least once. If no such subsequence exists, output 0.

sample

9
the quick brown fox jumps over the lazy dog
32