Problem C

Statement
Copy Copied
C. Monocarp's Stringtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMonocarp has a string $$$s$$$ of length $$$n$$$, consisting of the letters 'a' and 'b'. He wants to remove some (possibly zero) number of consecutive letters from his string in such a way that the number of letters 'a' and 'b' in the resulting string becomes equal. Monocarp can start removing letters from any position in the string $$$s$$$.Monocarp really likes his string $$$s$$$, so he wants to remove as few consecutive letters from it as possible.Your task is to determine the minimum number of consecutive letters from the string $$$s$$$ that need to be removed so that the number of letters 'a' and 'b' in the resulting string becomes equal. If it is necessary to remove all letters from the string $$$s$$$ (i.e., make it empty), report this.InputThe first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.Each test case consists of two lines:  the first line contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of characters in the string $$$s$$$;  the second line contains the string $$$s$$$ of length $$$n$$$, consisting of the letters 'a' and/or 'b'. Additional constraint on the input: the sum of values of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.OutputFor each test case, print the answer as follows:If in order to make the number of letters 'a' and 'b' equal, it is necessary to remove all letters from the string $$$s$$$, output $$$-1$$$.Otherwise, output the minimum number of consecutive letters that Monocarp needs to remove from his string $$$s$$$ so that the number of letters 'a' and 'b' becomes equal.ExampleInput55bbbab6bbaaba4aaaa12aabbaaabbaab5aabaaOutput30-12-1NoteIn the first example, Monocarp needs to remove the first three letters from his string. After that, his string will become "ab". In this string, there is one letter 'a' and one letter 'b'.In the second example, the given string has three letters 'a' and three letters 'b', so nothing needs to be removed.In the third example, all letters of the string need to be removed, as there are no letters 'b', so $$$-1$$$ should be printed.In the fourth example, Monocarp can, for example, remove the fifth and sixth letters from his string. Then his string will become "aabbabbaab". In this string, there are five letters 'a' and five letters 'b'.In the fifth example, all letters of the string need to be removed to make the number of letters 'a' and 'b' equal, so $$$-1$$$ should be printed.