Problem:
 Score : 300 points    Problem Statement There are N stones arranged in a row. Every stone is painted white or black.
A string S represents the color of the stones. The i -th stone from the left is white if the i -th character of S is . , and the stone is black if the character is # .  Takahashi wants to change the colors of some stones to black or white so that there will be no white stone immediately to the right of a black stone.
Find the minimum number of stones that needs to be recolored.      Constraints  1 \leq N \leq 2\times 10^5  S is a string of length N consisting of . and # .         Input Input is given from Standard Input in the following format:  N  S       Output Print the minimum number of stones that needs to be recolored.        Sample Input 1 3
#.#      Sample Output 1 1  It is enough to change the color of the first stone to white.       Sample Input 2 5
#.##.      Sample Output 2 2       Sample Input 3 9
.........      Sample Output 3 0

Solution (Python):
import sys,math,collections,itertools
input = sys.stdin.readline

N = int(input())
s = input().rstrip()
cntb =0
cntw = s.count('.')
ans =cntw
for i in range(len(s)):
    if s[i] == '
        cntb +=1
    else:
        cntw -=1
    ans = min(ans,cntb+cntw)
print(ans)

Solution (Python):
n = int(input())
s = input()

b_cnt = [0] * (n + 1)
for i in range(n):
    if s[i] == '
        b_cnt[i + 1] = b_cnt[i] + 1
    else:
        b_cnt[i + 1] = b_cnt[i]

ans = n
for i in range(n + 1):
    ans = min(ans, ((n - i) - (b_cnt[n] - b_cnt[i])) + b_cnt[i])

print(ans)

Solution (Python):
from itertools import accumulate
from collections import Counter
N = int(input())
S = list(input())
kuro = [0]*N
shiro = [0]*N
ans = N

for i in range(N):
    if S[i] == "
        kuro[i] = 1
    else:
        shiro[i] = 1
shiro.reverse()
shiro = list(accumulate(shiro))
kuro = list(accumulate(kuro))

for i in range(N-1):
    ans = min(ans, kuro[i]+shiro[N-i-2])

ans = min(ans, kuro[N-1])
S = Counter(S)
ans = min(ans, S["
print(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
S = list(input())

lis = [1 if s=='
from itertools import accumulate
lis = list(accumulate(lis))

ans = 10**18
cnt = 0
for i in range(N-1, -1, -1):
    s = S[i]
    ans = min(ans, cnt+lis[i])
    if s=='.':
        cnt += 1
ans = min(ans, cnt)

print(ans)

Solution (Python):
def main():
    n = int(input())
    S = input()
    l, r = 0, S.count('.')
    ans = l + r
    for i in range(n):
        if S[i] == '
        else: r -= 1
        ans = min(ans, l + r)
    print(ans)
    
if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = input()

num_black = 0
num_white = 0

left_black = [0] * (n+1)

left_white = [0] * (n+1)
for i in range(n):
    if s[i] == '
        num_black += 1
    else:
        num_white += 1
    left_black[i+1] = num_black
    left_white[i+1] = num_white

right_white = [0] * (n+1)
for i in range(n,-1,-1):
    right_white[i] = num_white - left_white[i]


ans = num_white
for i in range(n+1):
    num_to_change = left_black[i] + right_white[i]
    ans = min(ans, num_to_change)
print(ans)

Solution (Python):
N = int(input())
S = input()

b, w = 0, 0
for i in range(N):
    if S[i] == '
        b += 1
    else:
        w += 1

ans = [0 for _ in range(N+1)]
ans[0], ans[-1] = w, b
for i in range(1, N):
    if S[i-1] == '
        ans[i] = ans[i-1] + 1
    else:
        ans[i] = ans[i-1] - 1

print(min(ans))

Solution (Python):
n = int(input())
s = input()
wh = [0] * n
bl = [0] * n

w , b = 0,0
for i in range(n):
  if s[i] == '.':
    w += 1
  else:
    b += 1
  wh[i] = w
  bl[i] = b
ans = n
wtot = wh[-1]
btot = bl[-1]
for i in range(n):
  x = (wtot - wh[i]) + bl[i]
  
  ans = min(ans, x)
ans = min(ans, wtot)
print (ans)

Solution (Python):

import bisect

N = int(input())
S = '0'+input()+'0'

lst1 = []
lst2 = [0]*(N+2)


for i in range(1,N+1):
  if S[i] == '
    lst1.append(i)
  if S[N+1-i] == '.':
    lst2[N+1-i] = lst2[N+2-i] + 1
  else:
    lst2[N+1-i] = lst2[N+2-i]
    

if len(lst1) == N or lst2[1] == N:
  print(0)
  exit()
  
rlt = N+1
for i in range(len(lst1)):
  rlt =  min(rlt, i+lst2[lst1[i]])
  
rlt = min(len(lst1), rlt)
  
print(rlt)

Solution (Python):
N = int(input())
S = input()

if len(set(S)) == 1:
    print(0)
    exit()

b = 0
w = 0
stones = [(0,0)]
for i in S:
    if i == "
        b += 1
    else:
        w += 1
    stones.append((b,w))

bb = S.count("
ww = S.count(".")

ans = 1e9

for b,w in stones:
    ans = min(ans,b+ww-w)

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline


def main():
    n = int(input().strip())
    S = input().strip()
    query_table = [0] * (n+1)
    for i, s in enumerate(S):
        if s == "
            query_table[i + 1] = query_table[i] + 1
        else:
            query_table[i + 1] = query_table[i]
    ans = float("inf")

    for ptr in range(n+1):
        
        left_black = query_table[ptr]
        left_white = ptr - left_black

        
        right_black = query_table[n] - left_black
        right_white = n - ptr - right_black
        ans = min(ans, left_black+right_white)

    print(ans)

    return


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
s = input()
ans = 0
c = 0
for i in range(n):
    if s[i] == "
        c += 1
    else:
        if c >= 1:
            ans += 1
            c -= 1
    
print(ans)

Solution (Python):
N = int(input())
S = list(input())

ans = 10**9

T = "
W = '.'
B = '

dp_blk = [0]*(N+1)
dp_wht = [0]*(N+1)

if all([B==c for c in S]) or all([W==c for c in S]):
    print(0)
    exit(0)

for ix in range(N):
    if S[ix] == B:
        dp_blk[ix+1] += dp_blk[ix] + 1
        dp_wht[ix+1] += dp_wht[ix]
    else:
        dp_wht[ix+1] += dp_wht[ix] + 1
        dp_blk[ix+1] = dp_blk[ix]




for lix in range(N+1):
    b_flip = dp_blk[lix]
    w_flip = dp_wht[N] - dp_wht[lix]
    flip = b_flip + w_flip
    
    if ans > flip:
        ans = flip




    
print(ans)

Solution (Python):
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n=int(input())
s=input()
mae=[0]*(n+2)
usiro=[0]*(n+2)
for i in range(1,n+1):
    if s[i-1]=="
        mae[i]+=1
    mae[i]+=mae[i-1]
    if s[n-i]==".":
        usiro[n-i+1]+=1
    usiro[n-i+1]+=usiro[n-i+2]

ans=2*n
for i in range(1,n+2):
    ans=min(ans,mae[i-1]+usiro[i])
print(ans)

Solution (Python):

"""
@author: H_Hoshigi
解説PDFを読んだ。
"""

def main():
    N = int(input())
    S = input()

    hash_counter = 0
    dot_counter = S.count(".")
    answer = dot_counter
    for i in range(N):
        if S[i] == "
            hash_counter += 1
        else:
            dot_counter -= 1
        answer = min(answer, hash_counter+dot_counter)
    print(answer)

if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
s = input()
cnt_w, cnt_b = s.count("."), 0
ans = cnt_w
if s.count(".") == n or s.count("
    ans = 0
else:
    for i in range(n):
        if s[i] == "
            cnt_b += 1
        else:
            cnt_w -= 1
        if i == n - 1 or s[i] != s[i + 1]:
            ans = min(ans, cnt_w + cnt_b)
print(ans)

Solution (Python):
import sys
from itertools import accumulate

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    s = input()

    black, white = [0] * n, [0] * n
    for i in range(n):
        if s[i] == "
            black[i] = 1
        else:
            white[i] = 1

    acc_b = [0] + list(accumulate(black))
    acc_w = [0] + list(accumulate(white))

    res = f_inf
    for left in range(n + 1):
        change_w = acc_b[left]
        change_b = acc_w[n] - acc_w[left]
        res = min(res, change_w + change_b)
    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    s = input()

    b = [0] * n
    cnt = 0
    for i in range(n):
        if s[i] == "
            cnt += 1
        b[i] = cnt

    w = [0] * n
    cnt = 0
    for i in reversed(range(n)):
        if s[i] == ".":
            cnt += 1
        w[i] = cnt

    res = f_inf
    w = w + [0]
    b = [0] + b
    for i, j in zip(w, b):
        res = min(res, i + j)
    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
n=int(input())
s=input()
b=[0 for i in range(n)]
tmp=0
for i in range(n-1,-1,-1):
    if s[i]=="
        tmp+=1
    b[i]=tmp
w=[0 for i in range(n)]
tmp=0
for i in range(n):
    if s[i]==".":
        tmp+=1
    w[i]=tmp
tmp=0
ans=float("inf")
for i in range(n):
    tmp=n-(w[i]+b[i])
    if tmp<ans:
        ans=tmp
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N = int(input())
S = input().rstrip()

lb = [0] * (N+1)
for i, c in enumerate(S, 1):
    lb[i] = lb[i-1] + (1 if c=='

ans = N + 100
bl = lb[-1]
for i in range(N+1):
    j = i+1
    left = lb[i]
    right = N - i - (bl - lb[i])
    ans = min(ans, left+right)
print(ans)

Solution (Python):
N = int(input())
S = input()
n = S.count('.')
ans = min(n, N-n)
lsc = 0
rsc = N-n
for i, c in enumerate(S):
    if c == '
        lsc += 1
        rsc -= 1
    a = lsc + ((N-i-1) - rsc)
    ans = min(ans, a)

print(ans)

Solution (Python):
from math import ceil,floor,comb,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
s = input()
a = [0]*(n+1)
b = [0]*(n+1)



for i in range(1, n+1):
    a[i] = a[i-1] + (1 if s[i-1] == '

for i in range(n-1, -1, -1):
    b[i] = b[i+1] + (0 if s[i] == '

c = [0]*(n+1)
for i in range(n+1):
    c[i] = a[i] + b[i]

print(min(c))

Solution (Python):
import sys
input = sys.stdin.readline
from itertools import accumulate


def read():
    N = int(input().strip())
    S = input().strip()
    return N, S


def solve(N, S):
    W = [0] * (N+1)  
    B = [0] * (N+1)  
    for i in range(N):
        s = S[i]
        W[i+1] = W[i] + 1 if s == "." else W[i]
        B[i+1] = B[i] + 1 if s == "
    
    
    ans = N
    for i in range(N+1):
        b2w = W[N] - W[i]
        w2b = B[i] - B[0]
        ans = min(ans, b2w + w2b)
    return ans


if __name__ == '__main__':
    inputs = read()
    print("%d" % solve(*inputs))

Solution (Python):
n = int(input())
stones = input()
white = stones.count('.')
black = 0
ans = white
for c in stones:
    if c == '
        black += 1
    else:
        white -= 1
    score = black + white
    ans = min(score, ans)
print(ans)

Solution (Python):
from collections import Counter
N = int(input())
S = list(input())
ans = 10 ** 9
hash = [0] * (N + 1)
memo = 0
for i in range(N):
  if S[i] == "
    memo += 1
  hash[i + 1] = memo
dot = [0] * (N + 1)
memo = 0
for i in range(N - 1,-1,-1):
  if S[i] == ".":
    memo += 1
  dot[i] = memo    
for i in range(N + 1):
  ans = min(ans, hash[i] + dot[i])
print(ans)

Solution (Python):
n = int(input())
s = input()

res = 1 << 40
num_black = 0
num_white = s.count('.')
for i in range(n):
    res = min(res, num_black + num_white)
    if s[i] == '.':
        num_white -= 1
    else:
        num_black += 1
        
res = min(res, num_black + num_white)
print(res)

Solution (Python):
n = int(input())
S = input()
cW = S.count('.')
cB = 0
ans = cW
for i in range(n):
    if S[i]=='
        cB += 1
    else:
        cW -= 1
    ans = min(ans, cW+cB)
print(ans)

Solution (Python):
n = int(input())
s = input()

a = [0] * (n + 1)
b = [0] * (n + 1)

for i in range(n):
    
    if s[i] == '
        a[i + 1] = 1
    
    if s[n - i - 1] == '.':
        b[n - i - 1] = 1
    
    a[i + 1] += a[i]
    b[n - i - 1] += b[n - i]

ans = 100100100

for i in range(n):
    
    ans = min(ans, a[i] + b[i + 1])

print(ans)

Solution (Python):
n=int(input())
s=input()
a=[0]
for i in s:
    if i==".":a+=[a[-1]+1]
    else:a+=[a[-1]]
w=s.count(".")
print(min(i-a[i]*2+w for i in range(n+1)))

Solution (Python):
n=int(input())
s=input()
a=[0]
for i in s:
    if i==".":a+=[a[-1]+1]
    else:a+=[a[-1]]
w=s.count(".")
print(min(i-a[i]+w-a[i] for i in range(n+1)))

Solution (Python):
ans=n=int(input())
s=input()
a=[0]
for i in s:
    if i==".":a+=[a[-1]+1]
    else:a+=[a[-1]]
w=s.count(".")
for i in range(n+1):
    ans=min(ans,i-a[i]+w-a[i])
print(ans)

Solution (Python):
ans=n=int(input())
s=input()
a=[0]
for i in s:
    if i==".":
        a+=[a[-1]+1]
    else:
        a+=[a[-1]]
w=s.count(".")
for i in range(n+1):
    ans=min(ans,i-a[i]+w-a[i])
print(ans)

Solution (Python):
n=int(input())
s=list(input())

from itertools import groupby

gr=groupby(s)

flg=0
black=[]
white=[]
for k,v in gr:
    if flg==0 and k=='
        flg=1
        black.append(len(list(v)))
    elif flg==1:
        if k=='
            black.append(len(list(v)))
        else:
            white.append(len(list(v)))
if len(black)==0 or len(white)==0:
    print(0)
    exit(0)




cnt=sum(white)
ans=[cnt]
for i in range(len(white)):
    cnt+=(black[i]-white[i])
    ans.append(cnt)

print(min(ans))

Solution (Python):
N = int(input())
S = input()

tmp = 10 ** 9
cntB = S.count("
cnt = 0
for i, s in enumerate(S):
    if s == "
        tmp = min(tmp, cnt + N - i - (cntB - cnt))
        cnt += 1

print(min(cntB, tmp))

Solution (Python):
N=int(input())
S=input()
W=[0]*N
B=[0]*N
tmp=0
for i in range(N):
	if S[i]=="
		tmp+=1
	B[i]=tmp
tmp=0
for i in range(N):
	if S[i]==".":
		tmp+=1
	W[i]=tmp
Max_W=(max(W))
tmp=float("inf")
for i in range(N-1):
	tmp=min(tmp,B[i]+(Max_W-W[i]))
print(min(tmp,max(B),max(W)))

Solution (Python):
n, s = int(input()), input()

ans, lb, rw = s.count('.'), 0, s.count('.')
for i in s:
    if i == '
        lb += 1
    elif i == '.':
        rw -= 1
    ans = min(ans, lb + rw)
print(ans)

Solution (Python):
n = int(input())
s = list(input())
ans = 10**6
w = [0]
for i in range(n):
    w.append(w[i] + (s[i] == "."))

for i in range(n + 1):
    ans = min(ans, i - w[i] + (w[-1] - w[i]))

print(ans)

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():

    N = in_n()
    S = list(in_s())[::-1]

    cb = [0] * (N + 1)
    cw = [0] * (N + 1)
    for i in range(N):
        if S[i] == '
            cb[i + 1] = 1
        else:
            cw[i + 1] = 1

    for i in range(N):
        cb[i + 1] += cb[i]
        cw[i + 1] += cw[i]

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

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
s=input()
accum=[0]*n
if s[0]=="
    accum[0]=1
for i in range(n-1):
    accum[i+1]+=accum[i]+(s[i+1]=="
accum=[0]+accum
ans=10**18
for i in range(n+1):
    ans=min(ans,accum[i]+n-i-accum[n]+accum[i])

print(ans)

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    S = SS()

    white = S.count('.')
    black = 0
    ans = white + black
    for i in S:
        if i == '.':
            white -= 1
        else:
            black += 1
        ans = min(white + black, ans)

    print(ans)

if __name__ == '__main__':
    resolve()

Solution (Python):
N=int(input())
S=input()
Wcum=[0]*(N+1)
Bcum=[0]*(N+1)
for i in range(N):
    Wcum[i+1]=Wcum[i]+(S[i]=='.')
    Bcum[i+1]=Bcum[i]+(S[i]=='
ans=N
for i in range(N+1):
    ans=min(ans,(i-Wcum[i])+(N-i-(Bcum[N]-Bcum[i])))
print(ans)

Solution (Python):


def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    S=input()
    bk=S.count("
    ans=N-bk
    w=0
    for i in range(N):
        if S[i]=="
            bk-=1
        else:
            w+=1
        temp=(i+1-w)+(N-i-1-bk)
        ans=min(ans,temp)
    print(ans)
        

main()

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate 

from collections import deque,defaultdict,Counter
import decimal
import re











import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
S = list(input())
white = [0] * (n+1)
black = [0] * (n+1)
for i in range(n):
    white[i+1] = white[i]  + (1 if S[i] == '.' else 0)
    black[i+1] = black[i] + (1 if S[i] == '
res = float('inf')

for left in range(n+1):
    tmp = 0
    
    tmp += black[left] - black[0]
    tmp += white[n] - white[left]
    res = min(res,tmp)
print(res)

Solution (Python):
n = int(input())
S = input()

cnt = [0]*(n+1)
white = S.count(".")
black = 0
cnt[0] = white
for i in range(n):
    if S[i] == ".":
        white -= 1
    else:
        black += 1
    cnt[i+1] = black + white
print(min(cnt))

Solution (Python):


import sys
from typing import Any, Callable, Deque, Dict, List, Mapping, Optional, Sequence, Set, Tuple, TypeVar, Union































def main():
    Num = Union[int, float]
    mod = 1000000007                
    inf = float('inf')              
    
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def isp():   return input().split()
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    
    n = ii()
    s = input()
    L = list(map(lambda x: 1 if x == '
    black_accum_count = [0] * (n + 1)    
    for i in range(n):
        black_accum_count[i+1] = black_accum_count[i] + L[i]
    
    
    cnt = inf
    for i in range(n+1):
        
        cnt = min(cnt, black_accum_count[i] + (n - i - (black_accum_count[n] - black_accum_count[i])))
    print(cnt)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
S = list(input())
left = [0] * N
if S[0] == '
    left[0] += 1
for i in range(1,N):
    left[i] = left[i-1]
    if S[i] == '
        left[i] += 1
if left[-1] == 0:
    print(0)
    exit()

right = [0] * N
if S[-1] == '.':
    right[-1] += 1
for i in reversed(range(N-1)):
    right[i] = right[i+1]
    if S[i] == '.':
        right[i] += 1
if right[0] == 0:
    print(0)
    exit()
ans = min(left[-1],right[0])
for i in range(N):
    if S[i] == '
        ans = min(ans,left[i]-1 + right[i])
print(ans)

Solution (Python):
n=int(input())
s=input()
w=s.count(".")
b=0
ans=10**9
for i in range(n):
    if w+b<ans: ans=w+b
    if s[i]==".": w-=1
    else: b+=1
if w+b<ans: ans=w+b
print(ans)

Solution (Python):
n = int(input())
S = input()

rw = S.count('.')
rb = S.count('
lw = 0
lb = 0

ans = min(rb, rw)
for s in S:
    if s == '.':
        lw += 1
        rw -= 1
    else:
        lb += 1
        rb -= 1
    ans = min(ans, lb+rw)
print(ans)

Solution (Python):
N = int(input())
S = list(input())
a = 0
b = 0
A = []
for i in range(N):
  if S[i] == '.':
    a += 1
  else:
    b += 1
  A.append(b-a)
A.sort()
ans = min(a, b, a+A[0])
print(ans)

Solution (Python):
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from math import factorial, sqrt, ceil, gcd
from functools import lru_cache, reduce
from decimal import Decimal
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def dijkstra_heap(s, edge, n):
    
    d = [10**20] * n
    used = [True] * n 
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr


def lcm(x, y):
    return (x * y) // gcd(x, y)


def lcm_list(numbers):
    return reduce(lcm, numbers, 1)


def gcd_list(numbers):
    return reduce(gcd, numbers)


def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True



def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P


def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret



n = int(input())
s = input()

cnt_w = s.count(".")
cnt_b = 0
ans = cnt_w + cnt_b
for i in range(1, n + 1):
    if s[i - 1] == "
        cnt_b += 1
    else:
        cnt_w -= 1
    ans = min(ans, cnt_w + cnt_b)
print(ans)

Solution (Python):
n = int(input())
s = input()

b = 0
w = s.count(".")
ans = b + w
for i in range(n):
    if s[i] == "
        b += 1
    else:
        w -= 1
    ans = min(ans, b+w)
    
print(ans)

Solution (Python):
N = int(input())
S = input()
B = [0 for _ in range(N+1)]
W = [0 for _ in range(N+1)]
for i in range(N):
    B[i+1] = B[i] + (S[i] == '
for i in range(N, 0, -1):
    W[i-1] = W[i] + (S[i-1] == '.')
print(min(B[i]+W[i] for i in range(N+1)))

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')
AtoZ = "abcdefghijklmnopqrstuvwxyz"






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)



def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  divisors.sort()
  return divisors


def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count




N = I()
S = S()
if S[0] == ".":
  L = [1]
else:
  L = [0]
for s in S[1:]:
  if s == ".":
    L.append(L[-1]+1)
  else:
    L.append(L[-1])

MIN = L[-1]
for i in range(N):
  MIN = min(MIN,(i+1)-L[i]+(N-i-1)-((N-i-1)-(L[-1]-L[i])))
print(MIN)

Solution (Python):
from itertools import accumulate


N = int(input())
S = input()
black = [0] * N
white = [0] * N
for i in range(N):
    if S[i] == "
        black[i] = 1
    else:
        white[i] = 1
black = list(accumulate(black, initial=0))
white = list(accumulate(white, initial=0))
ans = float("inf")
for i in range(N + 1):
    ans = min(ans, black[i] + white[N] - white[i])
print(ans)

Solution (Python):

from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right


def main():
    N = int(input())
    S = input()
    B = 0
    W = S.count('.')
    ans = B+W
    for i in range(N):
        if S[i] == '
            B += 1
        else:
            W -= 1
        ans = min(ans, B+W)
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):

from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N = int(input())
    S = input()
    B = []
    W = []
    i = 0
    fin = None
    while i < N:
        if S[i] == '
            if i == N-1:
                B.append(1)
                fin = True
            else:
                tmp = 1
                while S[i+1] == '
                    tmp += 1
                    i += 1
                    if i == N-1:
                        fin = True
                        break
                B.append(tmp)
        else:
            if i == N-1:
                W.append(1)
                fin = False
            else:
                tmp = 1
                while S[i+1] == '.':
                    tmp += 1
                    i += 1
                    if i == N-1:
                        fin = False
                        break
                W.append(tmp)
        i += 1

    ans = sum(B)
    L = len(B)+len(W)
    B = B[::-1]
    W = W[::-1]
    now = ans
    if fin:
        for i in range(L):
            if i % 2 == 0:
                now -= B[i//2]
            else:
                now += W[i//2]
            ans = min(ans,now)
    else:
        for i in range(L):
            if i % 2 == 1:
                now -= B[i//2]
            else:
                now += W[i//2]
            ans = min(ans,now)
    print(ans)



    

if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
s = input()

cnt = [0] * (n + 1)

for i in range(1, n + 1):
    cnt[i] = cnt[i - 1]
    if s[i - 1] == ".":
        cnt[i] += 1

ans = float("inf")

for i in range(0, n + 1):
    tmp = cnt[n] - cnt[i]  
    tmp += i - cnt[i]  
    ans = min(ans, tmp)

print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7


N = INT()
S = list(input())

tmp = S.count(".")
num = [0]*N
num_sharp = 0
num_peri = 0
for i in range(len(S)):
	if S[i] == "
		num_sharp += 1
	else:
		num_peri += 1
	num[i] = num_sharp + tmp-num_peri

print(min(tmp, min(num)))