fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool isAlt(const string& s) {
  5. for (size_t i = 1; i < s.size(); ++i)
  6. if (s[i] == s[i - 1]) return false;
  7. return true;
  8. }
  9.  
  10. int minOps(const string& s) {
  11. if (isAlt(s)) return 0;
  12.  
  13. int c0 = 0, c1 = 0;
  14. for (char ch : s) ch == '0' ? c0++ : c1++;
  15.  
  16. return abs(c0 - c1) > 1 ? 2 : 1;
  17. }
  18.  
  19.  
  20.  
  21. int main() {
  22. int T;
  23. cin >> T;
  24. while (T--) {
  25. string S;
  26. cin >> S;
  27. cout << minOps(S) << endl;
  28. }
  29. return 0;
  30.  
  31. }
  32.  
Success #stdin #stdout 0.01s 5288KB
stdin
3
110010
0101
010111
stdout
1
0
2