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:
n = int(input())
s = list(input())
white = s.count(".")
black = s.count("#")

color_dict = [{"w": 0, "b": 0} for x in range(n)]
for index in range(n):
    if s[index] == ".":
        color_dict[index]["w"] += 1 if index == 0 else color_dict[index-1]["w"] + 1
        color_dict[index]["b"] = color_dict[index-1]["b"]
    else:
        color_dict[index]["b"] += 1 if index == 0 else color_dict[index-1]["b"] + 1
        color_dict[index]["w"] = color_dict[index-1]["w"]

res = float("inf")
for colors in color_dict:
    w = colors["w"]
    b = colors["b"]
    res = min(res, b+(white-w))
print(min(white, black, res))

Submission status: Accepted

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

Submission status: Accepted

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

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

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:
from itertools import accumulate

n = int(input())
s = input()
res = 10**5
cta = 0
ctb = 0
a = [0] * (n)
b = [0] * (n)
#a:i番目までの白の数、b:i番目までの黒の数
for i in range(n):
  if s[i] == ".":
    cta += 1
  else:
    ctb += 1
  a[i] = cta
  b[i] = ctb
#print(b)
#print(a)
for i in range(n):
  res = min(res,b[i]+a[-1]-a[i]) #i番目までの黒の数+i番目以降の白の数
  #print(res)
res = min(res,a[-1],b[-1])
print(res)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
dp = [0]*(N+1)
for i in S:
    if i == '.':
        dp[0] += 1
for i in range(1, N+1):
    if S[i-1] == '.':
        dp[i] = dp[i-1] - 1
    else:
        dp[i] = dp[i-1] + 1
print(min(dp))

Submission status: Accepted

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

#s = "........."

list_ = []
last = "."
counter = 0
for i in range(n):
    now = s[i]
    if now != last:
        list_.append(counter)
        counter = 0
    counter += 1
    last = now
list_.append(counter)

if len(list_)%2==1:
    list_.append(0)

#print(list_)


list_w = []
list_w.append(0)
list_w.append(0)
c_w = 0
list_b = []
list_b.append(0)
list_b.append(0)
c_b = 0
for i in range(0,len(list_),2):
    c_w += list_[-1*i-2]
    c_b += list_[i+1]
    list_w.append(c_w)
    list_b.append(c_b)
    
#print(list_w)
#print(list_b)

list_count = []
for i in range(0,len(list_w)):
    list_count.append(list_b[i]+list_w[-1*i-1])
    
#print(list_count)

print(min(list_count))

Submission status: Accepted

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

suu = []
s_count = S.count('.')
k_count = 0
suu.append(s_count + k_count)
for i in range(N):
    if S[i] == "#":
        k_count += 1
    else:
        s_count -= 1
    suu.append(k_count + s_count)

print(min(suu))

Submission status: Accepted

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

white_all = S.count('.')
ans = white_all

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
Black_num,White_num = 0,0

for i in range(N):
 if(S[i] == '#'): Black_num += 1
 elif(S[i] == '.'): White_num += 1

tmp_Black_num,tmp_White_num = 0,0
#ans : 境界の左の黒の数 + 境界の右の白の数
ans1 = White_num #一番左を境界にした時の右の白の数
ans2 = Black_num #一番右を境界にした時の左の黒の数
ans = min(ans1,ans2)
for i in range(N):
 if(S[i] == '#'): tmp_Black_num += 1
 elif(S[i] == '.'): tmp_White_num += 1
 tmp = tmp_Black_num #境界より左の黒 
 tmp += White_num - tmp_White_num #境界より右の白
 ans = min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    count_white = 0
    count_black = 0
    count_white_R = S.count('.')
    count_black_R = S.count('#')
    min_rev = count_black + count_white_R
    for i in range(len(S)):
        if S[i] == '#':
            count_black += 1
            count_black_R -= 1
        else:
            count_white += 1
            count_white_R -= 1
        rev = count_black + count_white_R
        min_rev = min(rev,min_rev)
    print(min(min_rev,S.count('.'),S.count('#')))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

penals1 = []
tmp1 = 0
penals2 = []
tmp2 = 0

# 半分まで準備
N_even = (N % 2 == 0)
half_N = N // 2 if N_even else (N-1) // 2 

if N == 1:
    min_val = 0
else:
    for i in range(half_N):
        if S[i] == "#":
            tmp1 += 1
        penals1.append(tmp1)

        if S[N-1-i] == ".":
            tmp2 += 1
        penals2.append(tmp2)

    if N_even:
        min_val = penals1[half_N-1] + penals2[half_N-1] 

        j = 1
        for i in range(half_N, N):
            if S[i] == "#":
                tmp1 += 1
            #penals1.append(tmp1)
            val1 = tmp1 + penals2[half_N-1-j]

            if S[N-1-i] == ".":
                tmp2 += 1
            #penals2.append(tmp2)
            val2 = penals1[half_N-1-j] + tmp2

            min_val = min(min_val, val1, val2)
            j += 1

    else:
        first = True
        j = 0

        # N = (N-1) // 2 + (N-1) // 2 + 1
        for i in range(half_N, N):
            if S[i] == "#":
                tmp1 += 1
            val1 = tmp1 + penals2[half_N-1-j]

            if S[N-1-i] == ".":
                tmp2 += 1
            val2 = penals1[half_N-1-j] + tmp2

            if first:
                min_val = min(val1, val2)
                first = False
            else:
                min_val = min(min_val, val1, val2)
            j += 1

    if S[N-1] == "#":
        tmp1 += 1
    if S[0] == ".":
        tmp2 += 1

    min_val = min(tmp1, tmp2, min_val)
print(min_val)

Submission status: Accepted

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

res = x

for c in s:
    if c == '#':
        x += 1
    else:
        x -= 1
    res = min(res , x)
print(res)

Submission status: Accepted

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

res = x

for c in s:
    if c == '#':
        x += 1
    else:
        x -= 1
    res = min(res , x)
print(res)

Submission status: Accepted

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

# leftB[i] == S[:i]中の'#'の個数
leftB = [0] * (N + 1)
for i in range(1, N + 1):
    leftB[i] = leftB[i - 1] + (1 if S[i - 1] == '#' else 0)

# rightW[i] == S[i:]中の'.'の個数
rightW = [0] * (N + 1)
for i in range(N - 1, -1, -1):
    rightW[i] = rightW[i + 1] + (1 if S[i] == '.' else 0)

# bはS[:i]を'.'にするコスト. wはS[i:]を'#' にするコスト
m = min(b + w for b, w in zip(leftB, rightW))
print(m)

Submission status: Accepted

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

ans = N
# b, w
right = [S.count('#')]
right.append(N-right[0])
left = [0, 0]

for s in S:
    ans = min(ans, left[0]+right[1])
    if s == '#':
        left[0] += 1
        right[0] -= 1
    else:
        left[1] += 1
        right[1] -= 1
print(min(ans, left[0]+right[1]))

Submission status: Accepted

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

w_N = S.count(".")
ans = w_N
b = 0
w = 0

for i, s in enumerate(S):
    if s == "#":
        b += 1
    if s == ".":
        w += 1
    ans = min(ans,b + w_N - w)

print(ans)

Submission status: Accepted

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

b = [0] * (N+1)
w = [0] * (N+1)

for i, s in enumerate(S):
    b[i+1] = b[i]
    w[i+1] = w[i]
    if s == "#":
        b[i+1] += 1
    if s == ".":
        w[i+1] += 1

ans = float('inf')

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

print(ans)

Submission status: Accepted

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

ans = S.count(".")
ans2 = ans

for i in S:
    if i == "#":
        ans2 += 1
    else:
        ans2 -= 1
    ans = min(ans, ans2)

print(ans)

Submission status: Accepted

Solution code:
# coding:utf-8

def main():
    N = int(input())
    s = input()
    count_white = 0
    count_black = 0
    count_border = N

    if (not "." in s) or (not "#" in s):
        print(0)
        return

    for i in s:
        if i == ".":
            count_white += 1
        elif i == "#":
            count_black += 1

    patern_border_white = 0
    patern_border_black = 0
    temp_border_white = 0
    temp_border_black = 0
    
    for i in range(N-1):
        if i == 0:
            if s[0] == "#":
                temp_border_white = 1
                temp_border_black = count_white

            else:
                temp_border_white = 0
                temp_border_black = count_white -1

        else :
            if s[i] == "#":
                temp_border_white = patern_border_white + 1
                temp_border_black = patern_border_black
            else :
                temp_border_white = patern_border_white
                temp_border_black = patern_border_black - 1

        if count_border > temp_border_white + temp_border_black :
            count_border = temp_border_white + temp_border_black

        patern_border_white = temp_border_white
        patern_border_black = temp_border_black

    print(min(count_white,count_black,count_border))
    return

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# coding:utf-8

def main():
    N = int(input())
    s = input()
    count_white = 0
    count_black = 0
    count_border = N
    # patern_border_white = [0]*N
    # patern_border_black = [0]*N

    if (not "." in s) or (not "#" in s):
        print(0)
        return

    for i in s:
        if i == ".":
            count_white += 1
        elif i == "#":
            count_black += 1

    patern_border_white = 0
    patern_border_black = 0
    temp_border_white = 0
    temp_border_black = 0
    
    for i in range(N-1):
        if i == 0:
            if s[0] == "#":
                # patern_border_white[0] = 1
                # patern_border_black[0] = count_white
                temp_border_white = 1
                temp_border_black = count_white

            else:
                # patern_border_white[0] = 0
                # patern_border_black[0] = count_white -1
                temp_border_white = 0
                temp_border_black = count_white -1

        else :
            if s[i] == "#":
                # patern_border_white[i] = patern_border_white[i-1] + 1
                # patern_border_black[i] = patern_border_black[i-1]
                temp_border_white = patern_border_white + 1
                temp_border_black = patern_border_black
            else :
                # patern_border_white[i] = patern_border_white[i-1]
                # patern_border_black[i] = patern_border_black[i-1] - 1
                temp_border_white = patern_border_white
                temp_border_black = patern_border_black - 1

        if count_border > temp_border_white + temp_border_black :
            count_border = temp_border_white + temp_border_black

        patern_border_white = temp_border_white
        patern_border_black = temp_border_black

    print(min(count_white,count_black,count_border))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# coding:utf-8

def main():
    N = int(input())
    s = input()
    count_white = 0
    count_black = 0
    count_border = N
    temp = ""
    patern3_white = [0]*N
    patern3_black = [0]*N
    # patern3 = []
    if (not "." in s) or (not "#" in s):
        print(0)
        return

    for i in s:
        if i == ".":
            count_white += 1
        elif i == "#":
            count_black += 1

    for i in range(N-1):
        if i == 0:
            if s[0] == "#":
                patern3_white[0] = 1
                patern3_black[0] = count_white
            else:
                patern3_white[0] = 0
                patern3_black[0] = count_white -1
        else :
            if s[i] == "#":
                patern3_white[i] = patern3_white[i-1] + 1
                patern3_black[i] = patern3_black[i-1]
            else :
                patern3_white[i] = patern3_white[i-1]
                patern3_black[i] = patern3_black[i-1] - 1
        # patern3.append(patern3_white[i] + patern3_black[i])

        if count_border > patern3_white[i] + patern3_black[i] :
            count_border = patern3_white[i] + patern3_black[i]

    # print(patern3_black)
    # print(patern3_white)
    # print(patern3)
    # print()
    # count_border = min(patern3)
    # print(count_white)
    # print(count_black)
    # print(count_border)
    print(min(count_white,count_black,count_border))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

black0 = 0
white0 = 0
for i in range(n):
    if s[i] == '#':
        black0 += 1
    else:
        white0 += 1
        
black = 0
white = 0
best = min(black0, white0)
for i in range(n):
    if s[i] == '#':
        best = min(best, black + white0 - white)
        black += 1
    else:
        white += 1
        
print(best)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
cum_sum = 0
black = s.count("#")
ans = n - black
for i, c in enumerate(s):
    if c == "#":
        cum_sum += 1
    ans = min(ans, cum_sum + n - i - 1 - (black - cum_sum))
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

black = [0] * n
white = [0] * n

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


black = [0] + black
black = list(accumulate(black))
white = white[::-1]
white = list(accumulate(white))[::-1] + [0]

# print(black, white)

ans = 1e10
for i in range(n+1):
    ans = min(ans, black[i] + white[i])

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

    res = dot = S.count(".")
    sha = 0
    # print(res, dot)

    for i in range(N):
        if S[i] == "#":
            sha += 1
        else:
            dot -= 1
        tmp = sha + dot
        res = min(tmp, res)
    print(res)
main()

Submission status: Accepted

Solution code:
from itertools import accumulate

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

black = [0] * n
white = [0] * n

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


black = [0] + black
black = list(accumulate(black))
white = [0] + white[::-1]
white = list(accumulate(white))[::-1]

# print(black, white)

ans = 1e10
for i in range(n+1):
    ans = min(ans, black[i] + white[i])

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
input_str = input()
left_black = 0
right_white = input_str.count('.')
ans = [left_black + right_white]

for s in input_str:
  if( s == '#' ):
    left_black += 1
  if( s == '.' ):
    right_white -= 1
  ans.append(left_black + right_white)
print(min(ans))

Submission status: Accepted

Solution code:
import sys
from sys import stdin
N = int(stdin.readline().rstrip())
S = list(stdin.readline().rstrip())
res = 10**9
ctb = 0
ctw = 0
bs = [0]*N
ws = [0]*N
for i in range(N):
    if S[i] == '.':
        ctw += 1
    else:
        ctb += 1
    bs[i] = ctb
    ws[i] = ctw
for i in range(N):
    res = min(res, bs[i]+ws[-1]-ws[i])
res = min(res, bs[-1], ws[-1])
print(res)

Submission status: Accepted

Solution code:
n = int(input())
s = '.'+input()
lb, rw = 0, s.count('.')-1
ans = lb+rw
for i in range(1,n+1):
  if s[i] == '#':
    lb += 1
  elif s[i] == '.':
    rw -= 1
  if lb+rw < ans:
    ans = lb+rw
print(ans)

Submission status: Accepted

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

res = dot = S.count(".")
sha = 0
for i in range(N):
    if S[i] == "#":
        sha += 1
    else:
        dot -= 1
    tmp = sha + dot
    res = min(res, tmp)

print(res)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
ans = N
sumB = [0] * (N + 1)
tmp = 0
for i in range(N):
    if S[i] == '#':
        tmp += 1
    sumB[i+1] = tmp
for i in range(N+1):
    ans = min(sumB[i] + (N - i) - (sumB[N] - sumB[i]), ans)
    # print(sumB[i] , (N - i), (sumB[N] - sumB[i]), ans)
print(ans)

Submission status: Accepted

Solution code:
# https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = s_inpl()   |	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = inpl()	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]

import math
import copy
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
from bisect import bisect_left, bisect_right
# import numpy as np

# 四方向
dy = [0, 0, 1, -1]
dx = [1, -1, 0, 0]
INF = float("inf")

def i_inpl(): return int(input())
def s_inpl(): return map(int,input().split())
def l_inpl(): return list(map(int, input().split()))

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

l = [0] * (N+1)

for i, si in enumerate(S):
    if si == "#":
        l[i+1] = l[i] + 1
    else:
        l[i+1] = l[i]

ans = INF
for i in range(0, N+1):
    l_black = l[i]
    r_white = (N - i) - (l[-1] - l[i])
    ans = min(ans, (l_black + r_white))

print(ans)

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/tenka1-2019-beginner/submissions/5063821
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
    elif s==".":
        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 = list(input())
ans = white = s.count('.')
black = 0
for i in s:
    if i == '.':
        white -= 1
    else:
        black += 1
    ans = min(ans,white+black)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
bcnt = [0]*n
wcnt = [0]*n
result = 2 * 10 ** 5

# 累積和
if s[0] == "#":
    bcnt[0] = 1
if s[n-1] == ".":
    wcnt[n-1] = 1

for i in range(1,n):
    bcnt[i] = bcnt[i-1]
    if s[i] == "#":
        bcnt[i] += 1
for i in range(n-2,-1,-1):
    wcnt[i] = wcnt[i+1]
    if s[i] == ".":
        wcnt[i] += 1

# 最小コストを求める
for i in range(0,n-1):
    result = min(bcnt[i] + wcnt[i+1],result)

result = min(result,bcnt[n-1])
result = min(result,wcnt[0])

print(result)

Submission status: Accepted

Solution code:
SIRO = '.'
KURO = '#'
N = int(input())
S = input()
sCnt = S.count(SIRO)
kCnt = 0
ans = sCnt
for i in S:
    if i == KURO:
        kCnt += 1
    else:
        sCnt -= 1
    ans = min(ans, kCnt + sCnt)

print(ans)

Submission status: Accepted

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

# . : white
# # : black
# no '#.' sequence

# a list that represents no of stones seen up to the index
nwhite, nblack = [], []

nwhite_current, nblack_current = 0, 0

for c in S:
    if c == '.':
        nwhite_current += 1
    else:
        nblack_current += 1

    nwhite.append(nwhite_current)
    nblack.append(nblack_current)

ans = 0

if nwhite_current != 0 and nblack_current != 0:

    ans = N + 1

    # set a index that starts sequence of black(#)
    for i in range(N):

        ans_current = 0

        # flip all black(#) left to it
        if i > 0:
            ans_current = nblack[i-1]

        # flip all white(.) right to it    
        ans_current += (nwhite_current - nwhite[i])

        ans = min(ans, ans_current)

print(ans)

Submission status: Accepted

Solution code:
from functools import reduce
import math

def main():
    # 文字列の2進数を数値にする
    # '101' → '5'
    # 文字列の頭に'0b'をつけてint()にわたす
    # binary = int('0b'+'101',0)

    # 2進数で立っているbitを数える
    # 101(0x5) → 2
    # cnt_bit = bin(5).count('1')
    
    # N! を求める
    # f = math.factorial(N)
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    INF = float("inf")
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    N = int(input().rstrip())
    S = list(_ for _ in input())
    
    # 基準点iより右にいる白石の数と基準点iより左にいる黒石の数の和が答え
    # i=0 のとき答えは文字列中の白石の数になる
    cnt_w = S.count('.') # iより右みいる白石の数
    cnt_b = 0 # iより左にいる文字列中の黒石の数
    ans = cnt_w 
    for i in range(N): # i番目の石を左に置くとき       
        if(S[i] == '#'): # 黒石を左に置く場合
            cnt_b +=1
        else: # 白石を左に置く場合
            cnt_w -=1
        ans = min(ans,cnt_b+cnt_w)
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)

N=_I()
S=input()
#print(S)

black=0
white=S.count(".")
ans=white
for idx in range(len(S)):
	if S[idx] == "#":
		black+=1
	else:
		white-=1
	ans=min(ans,black+white)


	
print(ans)

Submission status: Accepted

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

list_b = []
cnt_b = 0
list_w = []
cnt_w = 0
for i in range(n):
    if s[i] == "#":
        cnt_b += 1
    else:
        cnt_w += 1
    list_b.append(cnt_b)
    list_w.append(cnt_w)
ans = 2*10**5
for i in range(n):
    if ans > list_b[i] + list_w[n-1] - list_w[i]:
        ans = list_b[i] + list_w[n-1] - list_w[i]
print(min(ans,list_b[n-1],list_w[n-1]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
s = list(input())
res = 10**6
ctw = 0
ctb = 0
w = [0]*n
b = [0]*n

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

Submission status: Accepted

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

a = s.count('.')
b = 0

ans = a

for i in range(n):
    if s[i] == '#':
        b += 1
    else:
        a -= 1
    ans = min(ans,a+b)

print(ans)

Submission status: Accepted

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

left_black = 0
right_white = S.count('.')
ans = right_white

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

Submission status: Accepted

Solution code:
from itertools import accumulate

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

B = [0] * N
W = [0] * N

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

B = [0] + B
B = list(accumulate(B)) # -- 位置を左から進めて、左側にある黒の数
W = [0] + W[::-1]
W = list(accumulate(W))[::-1] # -- 位置を左から進めて、右側にある白の数

ans = []
for i in range(N+1):
    ans.append(B[i] + W[i])
print(min(ans))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
s = list(input())
res = 10**9

cta = 0
ctb = 0
a = [0]*n
b = [0]*n
for i in range(n):
    if s[i] == '.':
        cta += 1
    else:
        ctb += 1
    a[i] = cta
    b[i] = ctb
for i in range(n):
    res = min(res,b[i]+a[-1]-a[i])
res = min(res,a[-1],b[-1])
print(res)

Submission status: Accepted

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

res = 10**9

cta = 0
ctb = 0
a = [0]*N
b = [0]*N

for i in range(N):
    if S[i] == ".":
        cta += 1
    else:
        ctb += 1
    a[i] = cta
    b[i] = ctb
for i in range(N):
    res = min(res, b[i] + (a[-1] - a[i]))
res = min(res, a[-1], b[-1])
print(res)

Submission status: Accepted

Solution code:
N=input()
S=raw_input()
L=[0]

for i,x in enumerate(S):
	if x=="#":
		L.append( L[-1]+1 )
	else:
		L.append( L[-1] )
			

ans=float("inf")
for i in range(N+1):
	b=L[i]			# black on left
	w=N-i-(L[-1]-L[i])	# white on right
	ans=min(ans , b+w)

print ans

Submission status: Accepted

Solution code:
N=input()
S=raw_input()
L=[0]

for i,x in enumerate(S):
	if x=="#":
		L.append( L[-1]+1 )
	else:
		L.append( L[-1] )
			

ans=float("inf")
for i in range(N+1):
	b=L[i]	# black on left
	w=N-i-(L[-1]-L[i])
	ans=min(ans , b+w)

print ans

Submission status: Accepted

Solution code:
def C(N, S):
    n_diff_chars = S.count(".")
    cand_list = [n_diff_chars]
    for c in S:
        if c == ".":
            n_diff_chars -= 1
        else:
            n_diff_chars += 1
        cand_list.append(n_diff_chars)
    print(min(cand_list))


if __name__ == "__main__":
    N = int(input())
    S = input()
    C(N, S)

Submission status: Accepted

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

cnt = 0
cnt_black = 0
should_change = 0

cnt_black_arr = [0] * (N+1)

for i in range(N):
    if S[i] == '#':
        cnt_black += 1
    cnt_black_arr[i+1] = cnt_black

res = cnt_black
for i in range(N):
    num_black = N-i
    right_white = N - cnt_black - (i - cnt_black_arr[i])
    res = min(res, cnt_black_arr[i] + right_white)

print(res)

Submission status: Accepted

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

print(result)

Submission status: Accepted

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

check=s_input.count('.')
ans=check
for s in s_input:
    if s=='.':
        # 変えんでいい
        check-=1
    else:
        check+=1
    ans=min(ans,check)
print(ans)

Submission status: Accepted

Solution code:
def C(N, S):
    cand = "#" * N
    n_diff_chars = sum(a!=b for a, b in zip(cand, S))
    min_n_diff_chars = n_diff_chars
    for c in S:
        if c == ".":
            n_diff_chars -= 1
        else:
            n_diff_chars += 1
        min_n_diff_chars = min(min_n_diff_chars, n_diff_chars)
    print(min_n_diff_chars)


if __name__ == "__main__":
    N = int(input())
    S = input()
    C(N, S)

Submission status: Accepted

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

ans =  S.count(".")
l = [0, ans]
for i in range(N):
    if S[i] == "#":
        l[0] += 1
    else:
         l[1] -= 1
    
    if ans > sum(l):
        ans = sum(l)
print(ans)

Submission status: Accepted

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

CW = 0

for s in S:
    if s == '.' : CW += 1
    
CB = 0
ans = (CB+CW)
for s in S:
    if s == '.' : CW -= 1
    if s == '#' : CB += 1
    ans = min(ans,CB+CW)
    
print(ans)

Submission status: Accepted

Solution code:
N=input()
s=input()

N=int(N)
wn=[]
bn=[]
a=0
b=0

#各行間よりも前と後ろに何個白（黒）があるかをカウント
wn.append(0)
for i in range(N):
    j=N-(i+1)
    if(s[j]=="."):
        a+=1
    if(s[i]=="#"):
        b+=1
    bn.append(a)
    wn.append(b)

bn.reverse()
bn.append(0)
#print(bn)
#print(wn)

#各行間のscore算出
min=1000000
for i in range(N+1):
    score=bn[i]+wn[i]
    if(score<min):
        min=score

print(min)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
sr = s[::-1]

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
w = 0
b = 0

for i in s:
    if i == ".":
        w += 1

m = w + b

for i in range(0, n):
    if s[i] == ".":
        w -= 1
    else:
        b += 1
    if w + b < m:
        m = w + b

print(m)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
black = 0
ans = white = s.count('.')

for i in range(n):
    if s[i] == "#":
        black += 1
    else:
        white -= 1
    ans = min(ans, black+white)
print(ans)

Submission status: Accepted

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

b = [0]  # 黒の累積和
w = [0]  # 白の累積和

count_b = 0
count_w = 0
for i in range(n):
    if s[i] == "#":
        # 黒なら
        count_b += 1
    else:
        # 白なら
        count_w += 1
    b.append(count_b)
    w.append(count_w)

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

Submission status: Accepted

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

left_black = 0
right_white = 0

for s in S:
    if s == '.':
        right_white += 1

c_min = left_black + right_white

for i in range(N):
    if S[i] == '#':
        left_black += 1
    elif S[i] == '.':
        right_white -= 1
    c = left_black + right_white
    if c < c_min:
        c_min = c
print(c_min)

Submission status: Accepted

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

num = S.count('.')

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

print(min(anslist))