Find the longest substring of a given string with at most K unique characters.
Example:
("cabbacc", 1) -> "bb" or "cc"
("cabbacc", 2) -> "abba"
("cabbacc", 3) -> "cabbacc"
This problem is a classic sliding-window challenge: maintain a window with a frequency map of characters and keep expanding the right pointer while the number of distinct characters stays within K. When the window exceeds K unique characters, shrink it from the left until it becomes valid again. Track the best valid window length throughout the process. For the sample string "cabbacc", the answers are "bb" or "cc" for K=1, "abba" for K=2, and the entire string for K=3.