Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()

ans = N
for i in range(N + 1):
    left_num = S[:i].count('#')
    right_num = S[i:].count('.')

    if right_num == N - i:
        right_num = 0
    ans = min(ans, (left_num + right_num))
    if right_num in [0, N - i] or left_num > right_num:
        break
print(ans)

Fixed submission:
N = int(input())
S = input()

all_black_num = S.count('#')
all_white_num = N - all_black_num

if all_black_num == 0 or all_white_num == 0:
    ans = 0
else:
    left_black_num = 0
    left_white_num = 0
    right_white_num = all_white_num - left_white_num
    right_black_num = all_black_num - left_black_num
    ans = left_black_num + right_white_num
    for i, c in enumerate(S):
        if c == '#':
            left_black_num += 1
        elif c == '.':
            left_white_num += 1
        right_white_num = all_white_num - left_white_num
        right_black_num = all_black_num - left_black_num
        ans = min(ans, left_black_num + right_white_num)
        if right_black_num == N - i - 1:
            break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()

ans = N
for i in range(N + 1):
    left_num = S[:i].count('#')
    right_num = S[i:].count('.')
    if right_num in [0, N - i]:
        ans = min(ans, left_num)
        break
    else:
        ans = min(ans, (left_num + right_num))
print(ans)

Fixed submission:
N = int(input())
S = input()

all_black_num = S.count('#')
all_white_num = N - all_black_num

if all_black_num == 0 or all_white_num == 0:
    ans = 0
else:
    left_black_num = 0
    left_white_num = 0
    right_white_num = all_white_num - left_white_num
    right_black_num = all_black_num - left_black_num
    ans = left_black_num + right_white_num
    for i, c in enumerate(S):
        if c == '#':
            left_black_num += 1
        elif c == '.':
            left_white_num += 1
        right_white_num = all_white_num - left_white_num
        right_black_num = all_black_num - left_black_num
        ans = min(ans, left_black_num + right_white_num)
        if right_black_num == N - i - 1:
            break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()

ans = N
for i in range(N + 1):
    left_num = S[:i].count('#')
    right_num = S[i:].count('.')
    ans = min(ans, (left_num + right_num))
    if right_num == 0:
        break
print(ans)

Fixed submission:
N = int(input())
S = input()

all_black_num = S.count('#')
all_white_num = N - all_black_num

if all_black_num == 0 or all_white_num == 0:
    ans = 0
else:
    left_black_num = 0
    left_white_num = 0
    right_white_num = all_white_num - left_white_num
    right_black_num = all_black_num - left_black_num
    ans = left_black_num + right_white_num
    for i, c in enumerate(S):
        if c == '#':
            left_black_num += 1
        elif c == '.':
            left_white_num += 1
        right_white_num = all_white_num - left_white_num
        right_black_num = all_black_num - left_black_num
        ans = min(ans, left_black_num + right_white_num)
        if right_black_num == N - i - 1:
            break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()

ans = N
for i in range(N + 1):
    ans = min(ans, (S[:i].count('#') + S[i:].count('.')))
print(ans)

Fixed submission:
N = int(input())
S = input()

all_black_num = S.count('#')
all_white_num = N - all_black_num

if all_black_num == 0 or all_white_num == 0:
    ans = 0
else:
    left_black_num = 0
    left_white_num = 0
    right_white_num = all_white_num - left_white_num
    right_black_num = all_black_num - left_black_num
    ans = left_black_num + right_white_num
    for i, c in enumerate(S):
        if c == '#':
            left_black_num += 1
        elif c == '.':
            left_white_num += 1
        right_white_num = all_white_num - left_white_num
        right_black_num = all_black_num - left_black_num
        ans = min(ans, left_black_num + right_white_num)
        if right_black_num == N - i - 1:
            break
print(ans)