MicrosoftOA #10
Balance the Game Board
估计中等可在线判题
考点(AI 标注)模拟/实现前缀和区间
A token sits at one of the positions 0..N, and the letters lie between adjacent positions. Each step flips the a/b letter the token crosses. Find the minimum number of steps needed to make the counts of the two letters equal, or −1 if it is impossible.
输入格式
The first line contains the letter string L (1..100, only a/b), and the second line contains start (0..N).
输出格式
Output a single integer, the answer.
样例 1
输入:
aaabab
0
输出:
1
样例 2
输入:
aaabab
6
输出:
5
样例 3
输入:
ababa
1
输出:
-1
样例说明
Example 1: moving right across the first a turns the string into baabab, with 3 of each letter; the answer is 1.
Example 2: moving left 5 steps flips indices 1..5, giving abbaba with 3 of each letter; no shorter move balances the letters, so the answer is 5.
Example 3: the total number of letters, 5, is odd, so they cannot be split into equal counts; output −1.