Help me in solving FAIR_DISTRIB problem

My issue

I understand that there are a few cases

  1. 011: Not possible
  2. 0101 or 1101
  3. 1001
  4. 10001: Not possible

I can convince myself looking at these cases that

  1. We can’t get consecutive elements at the end
  2. We can’t get 3 consecutive same elements

But not 100% sure how to prove it mathematically. Please can someone help me?

My code

def is_fair_distribution(n, s):
    number = 0
    for i in range(n - 1, -1, -1):
        if s[i] == '0':
            number += 1
        else:
            number -= 1

        if number < -1 or number > 1:
            return "NO"

    return "YES"

t = int(input())

for w in range(t):
    n = int(input())
    s = input().strip()
    print(is_fair_distribution(n, s))

Problem Link: Fair Distribution Practice Coding Problem - CodeChef