Maximum frequency with substrings

Java Hard collections

Task «Maximum frequency with substrings»

Given a string S consisting of lowercase Latin letters. It is necessary to find the maximum length of a substring in which each character appears at most K times. If there are several such substrings, output any of them. Use a sliding window and a HashMap to track character frequencies. Constraints: the length of S is up to 10^5, K ranges from 1 to 10^5.

Input Format

One line: S and an integer K, separated by a space.

Output Format

One line: the desired substring. If there is no substring, output an empty line.

Examples

Example 1

INPUT
abcabcabc 2
OUTPUT
abcabc

Example 2

INPUT
2024-03-18
OUTPUT
понедельник

Example 3

INPUT
aaa 1
OUTPUT
a

Example 4

INPUT
2024-03-19
OUTPUT
вторник

Example 5

INPUT
abacabad 3
OUTPUT
bacabad
main.java