fork download
  1. import java.util.Scanner;
  2.  
  3. class Ideone {
  4. public static void main(String[] args) throws java.lang.Exception {
  5. Scanner sc = new Scanner(System.in);
  6. int t = sc.nextInt();
  7. while (t-- > 0) {
  8. String s = sc.next();
  9. StringBuilder str = new StringBuilder(s);
  10. int n = s.length();
  11. int ans = 0;
  12. int onesCount = 0;
  13.  
  14. // Count the number of ones in the string
  15. for (char c : s.toCharArray()) {
  16. if (c == '1') {
  17. onesCount++;
  18. }
  19. }
  20.  
  21. int zerosCount = n - onesCount;
  22. int minCount = Math.min(onesCount, zerosCount);
  23.  
  24. // The minimum total cost is the minimum of the number of ones and zeros in the string
  25. ans = minCount;
  26.  
  27. System.out.println(ans);
  28. }
  29. }
  30. }
  31.  
Success #stdin #stdout 0.14s 54616KB
stdin
1
101011

stdout
2