Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Problem D
Hidden Anagrams

An anagram is a word or a phrase that is formed by rearranging the letters of another. For instance, by rearranging the letters of "William Shakespeare," we can have its anagrams "I am a weakish speller," "I'll make a wise phrase," and so on. Note that when $A$ is an anagram of $B$, $B$ is an anagram of $A$.

In the above examples, differences in letter cases are ignored, and word spaces and punctuation symbols are freely inserted and/or removed. These rules are common but not applied here; only exact matching of the letters is considered.

For two strings $s_1$ and $s_2$ of letters, if a substring $s'_1$ of $s_1$ is an anagram of a substring $s'_2$ of $s_2$, we call $s'_1$ a hidden anagram of the two strings, $s_1$ and $s_2$. Of course, $s'_2$ is also a hidden anagram of them.

Your task is to write a program that, for given two strings, computes the length of the longest hidden anagrams of them.

Suppose, for instance, that "anagram" and "grandmother" are given. Their substrings "nagr" and "gran" are hidden anagrams since by moving letters you can have one from the other. They are the longest since any substrings of "grandmother" of lengths five or more must contain "d" or "o" that "anagram" does not. In this case, therefore, the length of the longest hidden anagrams is four. Note that a substring must be a sequence of letters occurring consecutively in the original string and so "nagrm" and "granm" are not hidden anagrams.

Input

The input consists of a single test case in two lines.

$s_1$

$s_2$

$s_1$ and $s_2$ are strings consisting of lowercase letters (a through z) and their lengths are between 1 and 4000, inclusive.

Output

Output the length of the longest hidden anagrams of $s_1$ and $s_2$. If there are no hidden anagrams, print a zero.

Sample Input 1

anagram
grandmother

Sample Output 1

4

Sample Input 2

williamshakespeare
iamaweakishspeller

Sample Output 2

18

Sample Input 3

aaaaaaaabbbbbbbb
xxxxxabababxxxxxabab

Sample Output 3

6

Sample Input 4

abababacdcdcd
efefefghghghghgh

Sample Output 4

0

Solution in JAVA (judge verdict: Runtime Error)

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashSet;
import java.util.StringTokenizer;

public class Main {
	
	public static class Pair implements Comparable<Pair> {
		int x, y;
		
		public Pair(int x, int y){
			this.x = x;
			this.y = y;
		}
		
		@Override public int compareTo(Pair arg0){
			return Integer.compare(this.x, arg0.x);
		}
	}

	public static void main(String[] args) throws IOException {
		final Scanner sc = new Scanner(System.in);
		
		final char[] s1 = sc.next().toCharArray();
		final char[] s2 = sc.next().toCharArray();
		
		final int min_len = Math.min(s1.length, s2.length);

		int[] count1 = new int[26];
		int[] count2 = new int[26];
		
		for(int len = min_len; len >= 1; len--){
			Arrays.fill(count1, 0);
			for(int i = 0; i < len; i++){
				count1[s1[i] - 'a']++;
			}
			
			for(int fst = 0; fst <= (s1.length - len); fst++){
				Arrays.fill(count2, 0);
				for(int j = 0; j < len; j++){
					count2[s2[j] - 'a']++;
				}
				

				for(int snd = 0; snd <= (s2.length - len); snd++){
					boolean ok = true;
					for(int k = 0; k < count1.length; k++){
						if(count1[k] != count2[k]){
							ok = false;
							break;
						}
					}
					
					if(ok){
						System.out.println(len);
						return;
					}
					
					count2[s2[snd] - 'a']--;
					if(snd + len < s2.length){
						count2[s2[snd + len] - 'a']++;
					}
				}
				
				count1[s1[fst] - 'a']--;
				if(fst + len < s1.length){
					count1[s1[fst + len] - 'a']++;
				}
			}
		}
		
		System.out.println(0);
	}

	public static class Scanner implements Closeable {
		private BufferedReader br;
		private StringTokenizer tok;

		public Scanner(InputStream is) throws IOException {
			br = new BufferedReader(new InputStreamReader(is));
		}

		private void getLine() throws IOException {
			while (!hasNext()) {
				tok = new StringTokenizer(br.readLine());
			}
		}

		private boolean hasNext() {
			return tok != null && tok.hasMoreTokens();
		}

		public String next() throws IOException {
			getLine();
			return tok.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public void close() throws IOException {
			br.close();
		}
	}
}