This problem needs more and better testcases (seems to be the case for many problems in this course). I see multiple comments about why is not a set used instead of a map, to track characters. This situation would easily be avoided with a testcase such as:
"rraaaci", 1
This should return 3, but when a set is used instead of a map, it returns 4.