题解
思路
模拟
统计 0 ,1 个数
枚举,如果还剩有1,可以填 num0–
同理剩余0,可以填 num1–
如果枚举到填不了的地方,就得把剩余的都删掉,因为这一位填不了,从 1 到这一位就不可能完全相反
#include <iostream>
#include <algorithm>
#include <string>
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int T;
int main()
{
cin>>T;
while(T--)
{
string a;
cin>>a;
int n0 = 0 , n1 = 0;
for(int i = 0; i < a.size();i++)
{
if(a[i] == '0')n0++;
else n1++;
}
int ans = 0;
for(int i = 0; i < a.size();i++)
{
if(a[i] == '0')
{
if(n1 == 0)break;
n1--;
}
else
{
if(n0 == 0)break;
n0--;
}
}
cout<<n0 + n1<<endl;
}
return 0;
}