-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB_Swap_and_Delete.cpp
53 lines (48 loc) · 1018 Bytes
/
B_Swap_and_Delete.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
//ismein bas zero aur one ka count rkh liya aur fir iterate kri string aur fir agar zero mila toh check kiya one se swap kr skte aur one mila toh check
//kiya zero se swap kr skte aur kisi bhi point par agar ni kr skte toh uske age ke sab delete hi krne padenge
int solve(string s){
int n = s.size();
if(n == 1){
return 1;
}
if(n == 0){
return 0;
}
int zeroes = 0;
int ones = 0;
for(auto ch:s){
if(ch == '0'){
zeroes++;
}else{
ones++;
}
}
int ans = 0;
for(int i=0;i<n;i++){
int element = s[i] - '0';
if(element == 0 && ones > 0){
ones--;
}else if(element == 1 && zeroes > 0){
zeroes--;
}else{
ans = n-i;
break;
}
}
return ans;
}
int main(){
int T;
cin>>T;
while(T--){
string s;
cin>>s;
int ans = solve(s);
cout<<ans<<endl;
}
return 0;
}