# most frequent character in a string after replacing all occurrences of x in a binary string | function to find the most frequent character after replacing x with either '0' or '1' according as per the given conditions ; store the count of 0 s and 1 s in the s ; count the frequency of 0 and 1 ; if the character is 1 ; if the character is 0 ; stores first occurence of 1 ; traverse the to count the number of x between two consecutive 1 s ; if the current character is not x ; if the current character is 1 , add the number of xs to count1 and set prev to i ; otherwise ; find next occurence of 1 in the string ; if it is found , set i to prev ; otherwise , break out of the loop ; store the first occurence of 0 ; repeat the same procedure to count the number of x between two consecutive 0 s ; if the current character is not x ; if the current character is 0 ; add the count of xs to count0 ; set prev to i ; otherwise ; find the next occurence of 0 in the string ; if it is found , set i to prev ; otherwise , break out of the loop ; count number of x present in the starting of the string as xxxx1 ... ; store the count of x ; increment count1 by count if the condition is satisfied ; count the number of x present in the ending of the as ... xxxx0 ; store the count of x ; increment count0 by count if the condition is satisfied ; if count of 1 is equal to count of 0 , prx ; otherwise , if count of 1 is greater than count of 0 ; otherwise , pr0 ; 
def maxOccurringCharacter(s):
    count0 = 0
    count1 = 0
    for i in range(len(s)):
        if (s[i] == '1'):
            count1 += 1
        elif (s[i] == '0'):
            count0 += 1
    prev = -1
    for i in range(len(s)):
        if (s[i] == '1'):
            prev = i
            break
    for i in range(prev + 1, len(s)):
        if (s[i] != 'X'):
            if (s[i] == '1'):
                count1 += i - prev - 1
                prev = i
            else:
                flag = True
                for j in range(i+1, len(s)):
                    if (s[j] == '1'):
                        flag = False
                        prev = j
                        break
                if (flag == False):
                    i = prev
                else:
                    i = len(s)
    prev = -1
    for i in range(0, len(s)):

        if (s[i] == '0'):
            prev = i
            break
    for i in range(prev + 1, len(s)):
        if (s[i] != 'X'):
            if (s[i] == '0'):
                count0 += i - prev - 1
                prev = i
            else:
                flag = True

                for j in range(i + 1, len(s)):
                    if (s[j] == '0'):
                        prev = j
                        flag = False
                        break
                if (flag == False):
                    i = prev
                else:
                    i = len(s)
    if (s[0] == 'X'):
        count = 0
        i = 0
        while (s[i] == 'X'):
            count += 1
            i += 1
        if (s[i] == '1'):
            count1 += count
    if (s[(len(s) - 1)] == 'X'):
        count = 0
        i = len(s) - 1
        while (s[i] == 'X'):
            count += 1
            i -= 1
        if (s[i] == '0'):
            count0 += count
    if (count0 == count1):
        print("X")
    elif (count0 > count1):
        print(0)
    else:
        print(1)


S = "XX10XX10XXX1XX"
maxOccurringCharacter(S)

# Expected output:
1