/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		String s = "abcde";
        int k = 2;

        Ans(s, k);
    }

    public static void Ans(String s, int k) {
        TreeMap<Character, Integer> map = new TreeMap<>();

        int left = 0;
        int maxLen = 0;
        int start = 0;

        for (int right = 0; right < s.length(); right++) {
            char ch = s.charAt(right);

            map.put(ch, map.getOrDefault(ch, 0) + 1);

            while (map.lastKey() - map.firstKey() > k) {
                char remove = s.charAt(left);

                map.put(remove, map.get(remove) - 1);

                if (map.get(remove) == 0) {
                    map.remove(remove);
                }

                left++;
            }

            if (right - left + 1 > maxLen) {
                maxLen = right - left + 1;
                start = left;
            }
        }

        System.out.println("Substring: " +
            s.substring(start, start + maxLen));
        System.out.println("Length: " + maxLen);
	}
}