Most frequent character in a string after replacing all occurrences of X in a Binary String

import java.io.*;
  
class GFG {
  
    
    
    
    
    public static void
    maxOccuringCharacter(String s)
    {
        
        
        int count0 = 0, count1 = 0;
  
        
        
        for (int i = 0;
             i < s.length(); i++) {                             if (s.charAt(i) == '1') {                 count1++;             }                             else if (s.charAt(i) == '0') {                 count0++;             }         }                     int prev = -1;            for (int i = 0;              i < s.length(); i++) {                if (s.charAt(i) == '1') {                 prev = i;                 break;             }         }                                       for (int i = prev + 1;              i < s.length(); i++) {                                          if (s.charAt(i) != 'X') {                                                                                        if (s.charAt(i) == '1') {                     count1 += i - prev - 1;                     prev = i;                 }                                     else {                                                                  boolean flag = true;                        for (int j = i + 1;                          j < s.length();                          j++) {                         if (s.charAt(j) == '1') {                             flag = false;                             prev = j;                             break;                         }                     }                                                                  if (!flag) {                         i = prev;                     }                                                                  else {                         i = s.length();                     }                 }             }         }                     prev = -1;         for (int i = 0; i < s.length(); i++) {                if (s.charAt(i) == '0') {                 prev = i;                 break;             }         }                                       for (int i = prev + 1;              i < s.length(); i++) {                             if (s.charAt(i) != 'X') {                                     if (s.charAt(i) == '0') {                                             count0 += i - prev - 1;                                             prev = i;                 }                                     else {                                                                  boolean flag = true;                        for (int j = i + 1;                          j < s.length(); j++) {                            if (s.charAt(j) == '0') {                             prev = j;                             flag = false;                             break;                         }                     }                                                                  if (!flag) {                         i = prev;                     }                                                                  else {                         i = s.length();                     }                 }             }         }                                       if (s.charAt(0) == 'X') {                             int count = 0;             int i = 0;             while (s.charAt(i) == 'X') {                 count++;                 i++;             }                                                       if (s.charAt(i) == '1') {                 count1 += count;             }         }                                       if (s.charAt(s.length() - 1)             == 'X') {                             int count = 0;             int i = s.length() - 1;             while (s.charAt(i) == 'X') {                 count++;                 i--;             }                                                       if (s.charAt(i) == '0') {                 count0 += count;             }         }                              if (count0 == count1) {             System.out.println("X");         }                              else if (count0 > count1) {
            System.out.println(0);
        }
  
        
        else
            System.out.println(1);
    }
  
    
    public static void main(String[] args)
    {
        String S = “XX10XX10XXX1XX”;
        maxOccuringCharacter(S);
    }
}