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



Sample input:
3
#.#


Sample output:
1

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-

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

    kuro_num = 0
    siro_num = 0
    for moji in S:
        if moji == "#":
            kuro_num += 1
        else:
            siro_num += 1

    """
    ######################
    .#####################
    ..####################
    (略)
    ..........############
    (略)
    ....................##
    .....................#
    ......................
    の全パターンのうち、最も回数が少ないものが答え
    """
    right_kuro_num = kuro_num
    right_siro_num = siro_num
    ans = siro_num
    for moji in S:
        if moji == "#":
            right_kuro_num -= 1
        else:
            right_siro_num -= 1
        num = right_siro_num + (kuro_num - right_kuro_num)
        ans = min(ans, num)

    print(ans)

"""
26
...##..##...##.....#..#..#
"""
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
s = stdin.readline().rstrip()
sum = 0
wh = s.count('.')
cnt = 0
for itr in range(N):
    if s[itr] == '#':
        cnt += 1
        continue
    if cnt > 0:
        sum += 1
        cnt -= 1
print(sum)

Submission status: Accepted

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

if N==1:
    print("0")
else:
    cnt_left=0
    cnt_right=S.count(".")
    cnt = cnt_left + cnt_right
    for i in range(N):
        if S[i] == "#":
            cnt_left += 1
        else:
            cnt_right -= 1

        if cnt_left + cnt_right < cnt:
            cnt = cnt_left + cnt_right
    print(cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
s = input()

bcnt = [0 for _ in range(n+1)]
wcnt = [0 for _ in range(n+1)]
b = 0
w = 0
for i, (c, r) in enumerate(zip(s, s[::-1])):
    b += 1 if c == '#' else 0
    bcnt[i+1] = b

    w += 1 if r == '.' else 0
    wcnt[n-i-1] = w

change = [b + w for b, w in zip(bcnt, wcnt)]
print(min(change))

Submission status: Accepted

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

b = [0] * N
for i, s in enumerate(S):
    if s == '#':
        b[i] = b[i-1] + 1
    else:
        b[i] = b[i-1]
b = [0] + b + [0]
ans = N
for i in range(N+1):
    ans = min(ans, b[i] + N - i - b[N] + b[i-1])

print(ans)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-

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

    kuro_num = 0
    siro_num = 0
    for moji in S:
        if moji == "#":
            kuro_num += 1
        else:
            siro_num += 1

    # ..........############ のパターン
    right_kuro_num = kuro_num
    right_siro_num = siro_num
    ans = siro_num
    for moji in S:
        if moji == "#":
            right_kuro_num -= 1
        else:
            right_siro_num -= 1
        num = right_siro_num + (kuro_num - right_kuro_num)
        ans = min(ans, num)

    print(ans)

"""
26
...##..##...##.....#..#..#
"""
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate
N = int(input())
S = input()

if S == '.'*N or S == '#'*N:
    print(0)
    sys.exit()

ind1 = 0
for i, x in enumerate(S):
    if x == '#':
        ind1 = i
        break
num = 0
for i, x in enumerate(S[::-1]):
    if x == '.':
        num = i
        break
ind2 = N - num
s = S[ind1:ind2]
if len(s) == 0:
    print(0)
    sys.exit()

a = s.split('.')
b = s.split('#')
aa = []
bb = []
for x in a:
    if len(x) != 0:
        aa.append(len(x))
for x in b:
    if len(x) != 0:
        bb.append(len(x))
c = [0] + list(accumulate(aa))
d = [0] + list(accumulate(bb))
ls = []
for i in range(len(c)):
    ls.append(c[i] + d[-1] - d[i])
print(min(ls))

Submission status: Accepted

Solution code:
from collections import Counter
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def ii(): return int(stdin.readline())
def li(): return map(int, stdin.readline().split())
def ns(): return stdin.readline().rstrip()
N = ii()
S = ns()
bs = [0] * (N + 1)
ws = [0] * (N + 1)
total = float("inf")
left_b = 0
right_w = 0
for i, s in enumerate(S):
    if s == "#":
        left_b += 1
    bs[i + 1] = left_b
for i in reversed(range(N)):
    if S[i] == ".":
        right_w += 1
    ws[i] = right_w

for i in range(N):
    total = min(total, ws[i+1] + bs[i])

print(total)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N=INT()
S=input()

sm1=[0]*(N+2)
for i in range(N):
    sm1[i+1]=sm1[i]
    if S[i]=='#':
        sm1[i+1]+=1
sm2=[0]*(N+2)
for i in range(N+1, 1, -1):
    sm2[i-1]=sm2[i]
    if S[i-2]=='.':
        sm2[i-1]+=1

ans=INF
for i in range(N+1):
    ans=min(ans, sm1[i]+sm2[i+1])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r = s.count(".")
l = 0

ans = l+r
for i in s:
    if i == "#":
        l += 1
    else:
        r -= 1
    ans = min(ans,l+r)
print(ans)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-

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

    kuro_num = 0
    siro_num = 0
    for moji in S:
        if moji == "#":
            kuro_num += 1
        else:
            siro_num += 1

    if siro_num == N:
        print(0)
        exit()

    # ..........############ のパターン
    right_kuro_num = kuro_num
    right_siro_num = siro_num
    ans = siro_num
    for moji in S:
        if moji == "#":
            right_kuro_num -= 1
        else:
            right_siro_num -= 1
        num = right_siro_num + (kuro_num - right_kuro_num)
        ans = min(ans, num)

    print(ans)

"""
26
...##..##...##.....#..#..#
"""
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
n=int(input())
s=input()
w = s.count('.')
b = 0
ans=w+b
for i in s:
    if i=='#':
        b+=1
    else:
        w-=1
    if ans>=b+w:
        ans=b+w
print(ans)

Submission status: Accepted

Solution code:
# 解説を見て書いたコード
"""
ある境目を考える。境目より左が全て白、境目より右が全て黒でなくてはいけない。
ゆえに、境目より左の黒の数と境目より右の白の数を数えれば、その境目における「変更が必要な石の数」がわかる。
よって、境目を端から端まで動かしてその中で最小の「変更が必要な石の数」を求めればよい。
"""
N = int(input())
str = input()
minDistance = 0

black = "#"
white = "."
leftblack = 0
rightwhite = 0

for i in range(N):
    if str[i] == white:
        rightwhite += 1

minDistance += rightwhite

for j in range(0, N):
    if str[j] == black:
        leftblack += 1
    else:
        rightwhite -= 1
    if leftblack + rightwhite < minDistance:
        minDistance = leftblack+rightwhite

print("{}".format(minDistance))

Submission status: Accepted

Solution code:
N=int(input())
S=input()
A=[]
a=S.count(".")
A.append(a)
for i in range(N):
    if S[i]=="#":
        a=a+1
    else:
        a=a-1
    A.append(a)
print(min(A))

Submission status: Accepted

Solution code:
# 今のままだと駄目
"""
ある境目を考える。境目より左が全て白、境目より右が全て黒でなくてはいけない。
ゆえに、境目より左の黒の数と境目より右の白の数を数えれば、その境目における「変更が必要な石の数」がわかる。
よって、境目を端から端まで動かしてその中で最小の「変更が必要な石の数」を求めればよい。
"""
N = int(input())
str = input()
minDistance = 0

black = "#"
white = "."
leftblack = 0
rightwhite = 0

for i in range(N):
    if str[i] == white:
        rightwhite += 1

minDistance += rightwhite

for j in range(0, N):
    if str[j] == black:
        leftblack += 1
    else:
        rightwhite -= 1
    if leftblack + rightwhite < minDistance:
        minDistance = leftblack+rightwhite

print("{}".format(minDistance))

Submission status: Accepted

Solution code:
N=int(input())
S=input()
A=[]
r=S.count(".")
result=r
for i in range(N):
    if S[i]=="#":
        r=r+1
    else:
        r=r-1
    result=min(result,r)
print(result)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
r=s.count(".")
ans=[r]
for i in s:
    if i=="#":
        r+=1
    else:
        r-=1
    ans.append(r)
print(min(ans))

Submission status: Accepted

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

blk=0
white=S.count(".")

ans=white

for n in range(N):
	if S[n]=="#":
		blk += 1
	else:
		white -= 1
	# print(blk,white)
	ans=min(blk+white,ans)


print(ans)

Submission status: Accepted

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

bcount = 0
wcount = 0
mins = []

for i in range(N):
    if S[i] == ".":
        wcount += 1

mins.append(wcount)
        
for i in range(N):
    if S[i] == '#':
        bcount += 1
    else:
        wcount -= 1
    mins.append(bcount + wcount)

print(min(mins))

Submission status: Accepted

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

w = S.count('.')
b = 0

k = w

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

    k = min(k, w + b)

print(k)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-

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

    kuro_num = 0
    siro_num = 0
    for moji in S:
        if moji == "#":
            kuro_num += 1
        else:
            siro_num += 1

    if siro_num == N:
        print(0)
        exit()

    # ..........############ のパターン
    rest_kuro_num = kuro_num
    rest_siro_num = siro_num
    ans = siro_num
    for i in range(0, len(S)):
        num = rest_siro_num
        if S[i] == "#":
            num += 1 + (kuro_num - rest_kuro_num)
            rest_kuro_num -= 1
        else:
            num -= 1
            num += kuro_num - rest_kuro_num
            rest_siro_num -= 1
        ans = min(ans, num)

    print(ans)

"""
26
...##..##...##.....#..#..#
"""
if __name__ == "__main__":
    solve()

Submission status: Accepted

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

left = 0
right = s.count(".")
res = min(right, s.count("#"))
for i in range(n):
    if s[i] == "#":
        left += 1
    else:
        right -= 1
    res = min(res, left+right)

print(res)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
left = 0
right = S.count(".")
ans = left + right

for c in S:
    if c == "#":
        left += 1
    else:
        right -= 1
    ans = min(ans, left + right)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
b = S.count("#")
w = S.count(".")
ans = min(b,w)
if b>=1 and w>=1:
  cost = N-w
  for i in range(N-1, -1, -1):
    if S[i]=='.':
      cost+=1
    else: 
      cost-=1
      ans = min(ans,cost)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
w = s.count('.')
b = 0
ans = w
for i in s:
  if i =='#':
    b += 1
    
  else:
    w -=1
  ans = min(ans,b+w)
print(ans)

Submission status: Accepted

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


w=s.count(".")
ans = w

for i in range(n):

    if s[i]=="#":
        w+=1
    if s[i]==".":
        w-=1
    ans=min(ans,w)
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = list(input())

result = l.count(".")
boundary = l.count(".")
for i in range(n):
    if l[i] == ".":
        boundary -= 1
    else:
        boundary += 1
    result = min(result, boundary)    
print(result)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
w = s.count('.')

ans = w
for i in s:
  if i =='#':
    w += 1
    
  else:
    w -=1
  ans = min(ans,w)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
b=s.count("#")
w=s.count(".")
M=n
noww=0
for i in range(n-1):
    if s[i]==".":
        noww+=1
        if s[i+1]=="#":
            M=min(M,(i-noww+1)+w-noww)

print(min(b,w,M))

Submission status: Accepted

Solution code:
n=int(input())
s=input()
w=b=0
mn=0
for sk in s:
    if sk=="#":
        b+=1
    else:
        w-=1
    if mn>b+w:
        mn=b+w
print(mn-w)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ans = cnt = S.count(".")
for s in S :
    if s == "#" :
        cnt += 1
    else :
        cnt -= 1
    ans = min(ans,cnt)
print(ans)

Submission status: Accepted

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

b = 0
w = S.count('.')
ans = w

for s in S:
  if s == '#':
    b += 1
  else:
    w -= 1
  ans = min(ans, b + w)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
m = s.count(".")
ans = m
for i in range(n):
    if s[i] == "#":
        m += 1
    else:
        m -= 1
    ans = min(ans, m)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
Bsum = [0] #境界より左にある黒の数
Wsum = [0] #境界より右にある白の数
for i in range(N):
    Bsum += [Bsum[-1]+(1 if S[i]=="#" else 0) ]
    Wsum += [Wsum[-1]+(1 if S[N-i-1]=="." else 0)]
print(min([Bsum[i]+Wsum[N-i] for i in range(N+1)]))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N=INT()
S=input()

sm1=[0]*N
if S[0]=='#':
    sm1[0]=1
for i in range(1, N):
    sm1[i]=sm1[i-1]
    if S[i]=='#':
        sm1[i]+=1
sm2=[0]*N
if S[-1]=='.':
    sm2[-1]=1
for i in range(N-2, -1, -1):
    sm2[i]=sm2[i+1]
    if S[i]=='.':
        sm2[i]+=1

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

Submission status: Accepted

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

    n_dot_right = S.count('.')
    n_hash_left = 0

    ans = [n_dot_right + n_hash_left]

    for c in S:
        if c == '.':
            n_dot_right -= 1
        else:
            n_hash_left += 1
        ans.append(n_dot_right + n_hash_left)

    print(min(ans))
    

main()

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
#n = len(s)

bl = s.count('#')
wh = s.count('.')
cnt = wh
mincnt = cnt
for i in range(n):
    if s[i] == '#':
        cnt += 1
    else:
        cnt -= 1
    mincnt = min(mincnt, cnt)

print(mincnt)

Submission status: Accepted

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

kind = []
stone = []
s = 0

for i in range(N):
    if i == 0:
        kind.append(S[i])
        s += 1
    elif S[i-1] != S[i]:
        stone.append(s)
        kind.append(S[i])
        s = 1
    else:
        s += 1
    if i == N-1:
        stone.append(s)

if len(kind)==1:
    print(0)
elif len(kind)==2 and kind[0]=='#':
    print(min(stone[0],stone[1]))
else: 
    min = 0
    # print(kind, stone)
    if kind[0] == '.':
        start = 1
    else:
        start = 0 
    # 最初の#郡より右にある.の数を数えて足す
    for i in range(start+1,len(stone),2):
        min += stone[i]

    # #の個数を持つstoneのindexがi
    for i in range(start,len(stone),2):
        if i==start:
            tmp = min
        else:
            tmp += stone[i-2]
            tmp -= stone[i-1]
        if tmp < min:
            min = tmp
    print(min)

Submission status: Accepted

Solution code:
N = int(input())
S = str(input())

S0 = S[0]
L = []
count = 1
Wk=[]
Bk=[]
if N>1:
    for i in range(1,N):
        if S[i-1]!=S[i]:
            L.append(count)
            count =1

        else:
            count +=1
        if i ==N-1:
            L.append(count)
else:
    L.append(1)
lenL=len(L)
Numw =0
Numb =0
Numx =0

if S0 == '.':
    Wk.append(L[0])
    Bk.append(0)
    if S[-1] =='.':
        for i in range(1,lenL):
            if i%2 == 0:
                Numw +=L[i]
                Wk.append(Wk[-1]+L[i])
                Bk.append(Bk[-1])

            else: 
                Numb +=L[i]
                Bk.append(Bk[-1]+L[i])
                Wk.append(Wk[-1])

        m=(min(Numw,Numb))

        tmp = float("inf")


        for k in range(lenL):
            s = Bk[k]+(Wk[-1]-Wk[k])
            tmp = min(tmp,s)
        print(min(tmp,m))
        
    else:

        for i in range(1,len(L)-1):
            if i%2 ==0:
                Numw +=L[i]
                Wk.append(Wk[-1]+L[i])
                Bk.append(Bk[-1])
            else:
                Numb +=L[i]
                Bk.append(Bk[-1]+L[i])
                Wk.append(Wk[-1])
        if Bk:
            Bk.append(Bk[-1]+L[-1])
        else:
            Bk.append(L[-1])
        if Wk:
            Wk.append(Wk[-1])
        else:
            Wk.append(0)

        m=(min(Numb,Numw))
        tmp = float("inf")

        for k in range(lenL):
            s = Bk[k]+(Wk[-1]-Wk[k])
            tmp = min(tmp,s)
        print(min(tmp,m))

         

else:
    if S[-1]=='.':
        for i in range(len(L)):
            if i%2 ==0:
                Numb += L[i]
                if Bk:
                    Bk.append(Bk[-1]+L[i])
                else:
                    Bk.append(L[i])
                if Wk:
                    Wk.append(Wk[-1])
                else:
                    Wk.append(0)

            else:
                Numw += L[i]
                if Bk:
                    Bk.append(Bk[-1])
                else:
                    Bk.append(0)
                if Wk:
                    Wk.append(Wk[-1]+L[i])
                else:
                    Wk.append(L[i])

        m=(min(Numb,Numw))
        tmp = float("inf")

        for k in range(lenL):
            s = Bk[k]+(Wk[-1]-Wk[k])
            tmp = min(tmp,s)
        print(min(tmp,m))

    else:
        for i in range(len(L)-1):
            if i%2 ==0:
                Numb += L[i]
                if Bk:
                    Bk.append(Bk[-1]+L[i])
                else:
                    Bk.append(L[i])
                if Wk:
                    Wk.append(Wk[-1])
                else:
                    Wk.append(0)

            else:
                Numw += L[i]
                if Bk:
                    Bk.append(Bk[-1])
                else:
                    Bk.append(0)
                if Wk:
                    Wk.append(Wk[-1]+L[i])
                else:
                    Wk.append(L[i])
        if Bk:
            Bk.append(Bk[-1]+L[-1])
        else:
            Bk.append(L[-1])
        if Wk:
            Wk.append(Wk[-1])      
        else:
            Wk.append(0)
        m=(min(Numb,Numw))
        tmp =float("inf")  


        for k in range(lenL):
            s = Bk[k]+(Wk[-1]-Wk[k])
            tmp = min(tmp,s)
        print(min(tmp,m))

Submission status: Accepted

Solution code:
N = int(input())
S = str(input())

listS = list(S)

result = listS.count('.')
boundary = listS.count('.')

for i in range(N):
    if listS[i] == '.':
        boundary -= 1
    else:
        boundary += 1
    result = min(result, boundary)

print(result)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
b,w,ans=0,0,0
w=S.count('.')
ans=w
for i in S:
	if i=='#':b+=1
	else:w-=1
	ans=min(b+w,ans)
print(ans)

Submission status: Accepted

Solution code:
"""
python3 a.py < input.txt
"""
import math
import itertools as it
import heapq

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

T=[S[0]]
U=[1]
for si in S[1:]:
    if T[-1] == si:
        U[-1] = U[-1]+1
    else:
        T.append(si)
        U.append(1)
M=len(T)

B=[U[i] if T[i] == "#" else 0 for i in range(M)]
sumB=[B[0]]
for b in B[1:]:
    sumB.append(sumB[-1]+b)

W=[U[i] if T[i] == "." else 0 for i in range(M)]
sumW=[W[-1]]
for w in reversed(W[:-1]):
    sumW.append(sumW[-1]+w)
sumW=list(reversed(sumW))

l=[sum(B), sum(W)]
for i in range(M):
    if T[i] == "#":
        l.append(sumB[max(0, i-1)] + sumW[min(i+1, M-1)])

#print(S)
#print(T)
#print("U   ",U)
#print("B   ",B)
#print("sumB",sumB)
#print("W   ",W)
#print("sumW",sumW)
#print("l   ",l)
print(min(l))

Submission status: Accepted

Solution code:
n=int(input())
s = input()
n=len(s)

left_black = [0 for i in range(n+1)]
right_white = [0 for i in range(n+1)]


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

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

ans=int(1e9)
for i in range(n+1):
    ans = min(ans, left_black[i]+right_white[i])

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
r=s.count(".")
a=r
for i in range(n):
    if s[i]=="#":
        r+=1
    else:
        r-=1
    a=min(a,r)
print(a)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
m = s.count('.')
ans = m
for i in range(n):
  if s[i] == '#':
    m = m + 1
  else:
    m = m - 1
  ans=min(m,ans)
print(ans)

Submission status: Accepted

Solution code:

def main():
    N=int(input())
    line = list(input())
    ans= line.count(".")
    num = ans
    for i in line:
        if i ==".":
            num -=1
        else:
            num +=1
        ans = min(ans,num)

    return ans


print(main())

Submission status: Accepted

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

c = S.count('.')
Min = c


for j in range(n):
    if S[j] == '#':
        c += 1
    else:
        c -= 1
    Min = min(Min, c)
        
print(Min)

Submission status: Accepted

Solution code:
# C after
n = int(input())
s = str(input())

count_dot = 0
for i in range(n):
    if s[i] == ".":
        count_dot += 1
count_sharp = 0

ans = n
# 0: ###
# 1: .##
# 2: ..#
# 3: ...
for i in range(n):
    ans = min(ans, count_dot+count_sharp)
    if s[i] == "#":
        count_sharp += 1
    else:
        count_dot -= 1
ans = min(ans, count_dot+count_sharp)
print(ans)

Submission status: Accepted

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

w = S.count('.')
b = S.count('#')

if w == 0 or b == 0:
    print(0)
    exit()

nw = w
nb = 0
ans = nw
for i in range(N):
    if S[i] == '.':
        nw -= 1
    else:
        nb += 1
    ans = min(ans, nb + nw)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = str(input())

a = S.count('.')
r = a

for i in range(N):
    if S[i] == '#':
        a += 1
    else:
        a -= 1
    r = min(r, a)

print(r)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
b = [0 for i in range(N+1)]
w = [0 for i in range(N+1)]
for i in range(N):
  b[i+1] = b[i] + (1 if S[i]=="#" else 0)
for i in range(N, 0, -1):
  w[i-1] = w[i] + (1 if S[i-1]=="." else 0)
print(min([b[i]+w[i] for i in range(N+1)]))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
lis = []
ws=s.count('.')
lis.append(ws)
bs = 0
for x in s:
    if x == '#':
        bs += 1
        lis.append(bs+ws)
    else:
        ws -= 1
    lis.append(bs+ws)
print(min(lis))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = [s.count(".")]
test = s.count(".")

for i in s:
    if i == ".":
        test -= 1
    else:
        test += 1
    ans.append(test)

print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
S = str(input())

s = S

# 前の方の#と後ろの方の.の数を数えて足したい
dot_num = s.count('.')
sharp_num = 0

ans = dot_num

for c in s:
    if c == '#':
        sharp_num = sharp_num + 1
    else:
        dot_num = dot_num - 1
    
    ans = min(ans, sharp_num + dot_num)
print(ans)

Submission status: Accepted

Solution code:
import re

n = int(input())
s = input()
left = 0
right = len(re.findall('\.', s))

cnt = [left + right]
for i in range(n):
    if s[i] == '#':
        left += 1
    else:
        right -= 1
    cnt.append(left + right)

print(min(cnt))

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
S = list(input())

white_count=0
black_count=0
white_acc_list=[0]
black_acc_list=[0]
for i in range(N):
    if(S[i]=="."):
        white_count+=1
    else:
        black_count+=1
    black_acc_list.append(black_count)
    white_acc_list.append(white_count)

v = []
for i in range(N+1):
    v.append(black_acc_list[i]+(white_acc_list[N]-white_acc_list[i]))


#print("black_acc_list:{}".format(black_acc_list))
#print("white_acc_list:{}".format(white_acc_list))
#print("v_list:{}".format(v))
print(np.min(v))

Submission status: Accepted

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

countW = 0
for i in range(N):
    if S[i] == '.':
        countW += 1
countB = N - countW

count = 0
counter = [countW]
for i in range(N):
    if S[i] == '#':
        count += 1
    else:
        countW -= 1
    counter.append(count + countW)

print(min(counter))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = s.count('.')
num = ans
for c in s:
  if c == '.':
    num -= 1
  else:
    num += 1
  
  ans = min(ans,num)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
s = [S[i: i + 1] for i in range(N)]
bcount = 0
wcount = 0
for word in s:
    if word == '.':
        wcount += 1
ans = wcount
preans = ans
for word in s:
    if word == '#':
        preans += 1
    else:
        preans -= 1
    ans = min(ans, preans)
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = [str(i) for i in input()]

    white = []
    black = []

    # 黒を数える
    black_num = 0
    for s in S:
        black.append(black_num)
        if s == "#":
            black_num += 1
    black.append(black_num)

    # 白を数える
    S.reverse()
    white_num = 0
    for s in S:
        white.append(white_num)
        if s == ".":
            white_num += 1
    white.append(white_num)
    white.reverse()

    min_num = N
    for i in range(N + 1):
        temp_sum = white[i] + black[i]
        if min_num > temp_sum:
            min_num = temp_sum

    print(min_num)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
p = 0
C = [[0,0]]
W = 0
for i in range(N):
    if p==0:
        if S[i] == "#":
            C[-1][0] += 1
        else:
            p = 1
            C[-1][1] += 1
            W += 1
    else:
        if S[i] == ".":
            C[-1][1] += 1
            W += 1
        else:
            C.append([1,0])
            p = 0
if C[0][0] == 0:
    W -= C.pop(0)[1]
if len(C) >0:
    if C[-1][1] == 0:
        C.pop(-1)
tmp = W
ans = W
for b,w in C:
    tmp += b
    tmp -= w
    ans = min(tmp,ans)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(input())
result = N
count = 0
result = N
white = [0]*(N+1)
for i in range(N):
	white[i+1] = white[i]
	if a[i] == ".":
		white[i+1] += 1
for i in range(N+1):
    count = i-white[i] + white[N] - white[i]
    if count < result:
        result = count
print(result)

Submission status: Accepted

Solution code:
# reffer https://atcoder.jp/contests/tenka1-2019-beginner/submissions/5060831
N = int(input())
S = [1 if x == '.'  else 0 for x in list(input())]

cost = sum(S)
min_cost = cost

for i in range(N):
    if S[i] == 0:
        cost += 1
    else:
        cost -= 1

    if min_cost > cost:
        min_cost = cost

print(min_cost)

Submission status: Accepted

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

p = s.count('.')
q = 0
r = p

for i in range(n):
    if s[i] == '#':
        q += 1
    else:
        p -= 1
    # r = min(r, p + q)
    r = r if r < p + q else p + q

print(r)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
r=s.count(".")
a=r
for i in range(n):
  if s[i]=="#":r+=1
  else:r-=1
  a=min(a,r)
print(a)

Submission status: Accepted

Solution code:
import sys

n = int(sys.stdin.readline())
s = sys.stdin.readline().strip()

bcnt = 0
ans = 0
for t in s:
    if t == '#':
        bcnt += 1
    else:
        if bcnt > 0:
            ans += 1
            bcnt -= 1

print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate


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

black = [0]*N
white = [0]*N

for i, s in enumerate(S):
    if s == '#':
        black[i] = 1
    else:
        white[i] = 1

# 累積和
black = [0] + black
black = list(accumulate(black))
white = [0] + white[::-1]
white = list(accumulate(white))[::-1]

ans = []
for i in range(N+1):
    ans.append(black[i] + white[i])

print(min(ans))

Submission status: Accepted

Solution code:
N=int(input())
S=input()
M=S.count('.')
ans=M

for i in range(N):
	if S[i] == '#':
		M = M+1
	else:
		M = M-1
	ans = min(M,ans)
print(ans)

Submission status: Accepted

Solution code:
argN = int(input())
aryS = list(input())

cnt = aryS.count(".") 
ans = cnt
for i in range(argN):
  if aryS[i] == ".":
    cnt -= 1
  elif aryS[i] == "#":
    cnt += 1

  if ans > cnt:
    ans = cnt

print(ans)

Submission status: Accepted

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

p = s.count('.')
q = 0
r = p

for i in range(n):
    if s[i] == '#':
        q += 1
    else:
        p -= 1
    r = min(r, p + q)
    # r = r if r < p + q else p + q

print(r)

Submission status: Accepted

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

ans = S.count(".")

printnum = ans

for c in S:
    if c == ".":
        printnum -= 1
    else:
        printnum += 1
    ans = min(ans,printnum)

print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate


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

black = [0 for _ in range(N)]
white = [0 for _ in range(N)]

for i, s in enumerate(S):
    if s == '#':
        black[i] = 1
    else:
        white[i] = 1

# 累積和
black = [0] + black
black = list(accumulate(black))
white = [0] + white[::-1]
white = list(accumulate(white))[::-1]

ans = []
for i in range(N+1):
    ans.append(black[i] + white[i])

print(min(ans))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

trial_num = int(input())
stones = list(input())

left_black_stone = 0
right_white_stone = stones.count(".")
min_turn_stones = right_white_stone

for i in range(trial_num):
    if(stones[i] == "#"):
        left_black_stone += 1
    else:
        right_white_stone -= 1

    tmp_min = left_black_stone + right_white_stone
    if(tmp_min < min_turn_stones):
        min_turn_stones = tmp_min


print(min_turn_stones)

Submission status: Accepted

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

all_count_b = s.count("#")
all_count_w = n - all_count_b

count_b = 0
count_w = all_count_w

minimum = min([all_count_b, all_count_w])

for s1, s2 in zip(s, s[1:]):
    if s1 == "#":
        count_b += 1
    else:
        count_w -= 1
        if s2 == "#":
            minimum = min([count_b + count_w, minimum])

print(minimum)

Submission status: Accepted

Solution code:
# coding: utf-8

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

lb = 0
rw = S.count('.')
ans = lb + rw

for i in range(len(S)):
  if S[i] == '.':
    rw -= 1
  else:
    lb += 1
  ans = min(ans, rw + lb)

print(ans)