Interval Composition
Time limit1sMemory limit256 MB
Find the maximum length L such that each of the two lowercase strings has a contiguous block of length L with the same letter counts.
- Level
Medium6 of 10
- Topics
- Prefix sum, Hash map, String
- Solved
- No attempts yet
Problem
Two devices A and B each emit one signal per second. The signals a device emitted form a sequence of lowercase letters in time order. For example, let the signal sequences and be the following.
- = [a, f, c, d, r, d, e, s, d, e, f, w, s, z, r]
- = [g, e, d, s, r, d, d, e, m, z, r]
An interval is a contiguous part of a sequence. Two intervals have the same composition when they hold the same letters with the same multiplicities, in any order. The two boxed intervals in the picture below have the same composition. The 4th letter through the 10th letter of , which is d, r, d, e, s, d, e, and the 2nd letter through the 8th letter of , which is e, d, s, r, d, d, e, both consist of three d, two e, one r and one s.

Two intervals with the same composition always have the same length. More than one such pair of intervals can exist. Find the longest pair of intervals with the same composition, one in each of the two signal sequences.
Input
The first line and the second line each hold one signal sequence as a string with no spaces. Both strings consist of lowercase English letters only. The lengths and of the two strings satisfy .
Output
Print on the first line the length of the longest pair of intervals with the same composition. If no such pair exists, print .