유의할 점은 처음과 끝이 인접한 것이기에 여기에 대한 경우도 계산해 주어야 한다.
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std;
string str;
int bCnt = 0;
int main()
{
cin >> str;
for (int i = 0; i < str.size(); i++)
{
if (str[i] == 'b')
{
bCnt++;
}
}
int minVal = 1e9;
for (int i = 0; i < str.size(); i++)
{
int cnt = 0;
int Idx = i;
int bcnt = bCnt;
while(bcnt--)
{
if (str[Idx] == 'a')
cnt++;
if (Idx == str.size() - 1)
Idx = 0;
else
Idx++;
}
minVal = min(cnt, minVal);
}
cout << minVal;
return 0;
}