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: Wrong Answer

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

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

if S[N-2] == "B" and S[N-1] == "W":
    print(min(w,b))
elif S[N-1] == "B":
    print(min(w,b-1))
else:
    print(min(w,b))

Submission status: Wrong Answer

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


if __name__ == '__main__':
    N = int(input())
    S = input()

    cnt_w = 0
    for s in S[1:]:
        if s == '.':
            cnt_w += 1
    cnt_b = 1 if S[0] == '#' else 0

    if (cnt_b == 1) and (cnt_w == 0):
        print(0)
        exit(0)
    if (cnt_b == 0) and (cnt_w == N - 1):
        print(0)
        exit(0)

    ans = 1000000000
    for i in range(0, N - 1):
        if S[i] == '#' and S[i + 1] == '.':
            ans = min(ans, cnt_b + cnt_w)
            cnt_w -= 1
        elif S[i] == '.' and S[i + 1] == '#':
            ans = min(ans, cnt_b + cnt_w)
            cnt_b += 1
        elif S[i] == '.' and S[i + 1] == '.':
            cnt_w -= 1
        elif S[i] == '#' and S[i + 1] == '#':
            cnt_b += 1

    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


if __name__ == '__main__':
    N = int(input())
    S = input()

    cnt_w = 0
    for s in S:
        if s == '.':
            cnt_w += 1

    ans = 1000000000
    cnt_b = 0
    flag = False
    for i in range(0, N - 1):
        if S[i] != S[i + 1]:
            flag = True
            ans = min(ans, cnt_b + cnt_w)
        elif S[i] == S[i + 1] and S[i] == '.':
            cnt_w -= 1
        elif S[i] == S[i + 1] and S[i] == '#':
            cnt_b += 1

    if flag:
        print(ans)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


if __name__ == '__main__':
    N = int(input())
    S = input()

    T = []
    res = []
    flag = False
    for s in S:
        if (s == '#') and (not flag):
            res.append('#')
        elif (s == '.') and (flag):
            res.append('.')
        elif (s == '#') and (flag):
            T.append(''.join(res))
            flag = False
            res = ['#']
        elif (s == '.') and (not flag):
            flag = True
            res.append('.')

    res = ''.join(res)
    if "#." in res:
        T.append(res)

    ans = 0
    for t in T:
        cnt_b = 0
        cnt_w = 0
        for t_ in t:
            if t_ == '#':
                cnt_b += 1
            else:
                cnt_w += 1
        ans += min(cnt_b, cnt_w)

    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


if __name__ == '__main__':
    N = int(input())
    S = input()
    S = [s for s in S]

    cnt = 0
    for i in range(0, N - 2):
        if S[i] == '.' and S[i + 1] == '#' and S[i + 2] == '.':
            S[i + 1] = '.'
            cnt += 1
        elif S[i] == '#' and S[i + 1] == '.' and S[i + 2] == '#':
            S[i + 1] = '#'
            cnt += 1
        elif S[i] == '#' and S[i + 1] == '.' and S[i + 2] == '.':
            S[i] = '.'
            cnt += 1

    if len(S) >= 2:
        if S[-2] == '#' and S[-1] == '.':
            S[-1] = '#'
            cnt += 1

    print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


if __name__ == '__main__':
    N = int(input())
    S = input()
    S = [s for s in S]

    cnt2 = 0
    for i in range(0, N - 1):
        if S[i] == '#' and S[i + 1] == '.':
            cnt2 += 1

    cnt1 = 0
    for i in range(0, N - 1):
        if S[i] == '#' and S[i + 1] == '.':
            S[i + 1] = '#'
            cnt1 += 1

    print(min(cnt1, cnt2))

Submission status: Wrong Answer

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


if __name__ == '__main__':
    N = int(input())
    S = input()
    S = [s for s in S]

    cnt1 = 0
    for i in range(0, N - 1):
        if S[i] == '#' and S[i + 1] == '.':
            S[i + 1] = '#'
            cnt1 += 1

    cnt2 = 0
    for i in range(0, N - 1):
        if S[i] == '#' and S[i + 1] == '.':
            S[i] = '.'
            cnt2 += 1

    print(min(cnt1, cnt2))

Submission status: Wrong Answer

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


if __name__ == '__main__':
    N = int(input())
    S = input()
    S = [s for s in S]

    cnt = 0
    for i in range(0, N - 1):
        if S[i] == '#' and S[i + 1] == '.':
            S[i + 1] = '#'
            cnt += 1

    print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
ans=10**9
w=[0]*n
b=[0]*n
cw,cb=0,0
for i in range(n):
  if s[i]==".":
    cw+=1
  else:
    cb+=1
  w[i]=cw
  b[i]=cb

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

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
from itertools import groupby
gr=groupby(s)
c=[]
for k,g in gr:
  c.append((k,len(list(g))))
# print(c)
b,w=0,0
b=s.count("#")

f=False
for k,v in c:
  if f:
    if k==".":
      w+=v
      f=False
  else:
    if k=="#":
      f=True
if s[-1]=="#":
  b-=c[-1][1]
print(min(b,w))

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
from itertools import groupby
gr=groupby(s)
c=[]
for k,g in gr:
  c.append((k,len(list(g))))
b,w=0,0
b=s.count("#")
if s[-1]=="#":
  b-=1
f=False
for k,v in c:
  if f:
    if k==".":
      w+=v
      f=False
  else:
    if k=="#":
      f=True
print(min(b,w))

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
from itertools import groupby
gr=groupby(s)
c=[]
for k,g in gr:
  c.append((k,len(list(g))))
b,w=0,0
b=s.count("#")
f=False
for k,v in c:
  if f:
    if k==".":
      w+=v
      f=False
  else:
    if k=="#":
      f=True
print(min(b,w))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
from collections import Counter

a = 0
e = 0
for i in range(n):
    if s[i] == ".":
        a += 1
    else:
        break
for i in range(n):
    if s[n-1-i] == "#":
        e += 1
    else:
        break        

        
c = Counter(s[a:n])
d = Counter(s[:n-e])

print(min(c["."],d["#"]))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
from collections import Counter

a = 0
for i in range(n):
    if s[i] == ".":
        a += 1
    else:
        break
        
b = s[a:n]
c = Counter(b)

print(c["."])

Submission status: Wrong Answer

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

from collections import Counter

b = Counter(s)

print(min(b["."],b["#"]))

Submission status: Wrong Answer

Solution code:
N=int(input())
S=input()
l=[]
for i in range(N-1):
  if S[i:i+2]=='.#':
    l.append(i)
ans=[]
if l==[]:
  print(0)
else:
  for j in range(len(l)):
    ans.append(S[:i].count('#')+S[i+2:].count('.'))
  print(min(ans))

Submission status: Wrong Answer

Solution code:
N=int(input())
s=input()
S=[]
for i in range(N):
  S.append(s[i])
if '#' in s:
  for i in range(N):
    if s[i]=='#':
      t=i
      break
else:
  t=N 
c0=s[t:N].count('.')
c1=0
for i in range(N-2,-1,-1):
  if S[i]=='#' and S[i+1]=='.':
    S[i]='.'
    c1+=1
print(min(c0,c1))

Submission status: Wrong Answer

Solution code:
N=int(input())
s=input()
S=[]
for i in range(N):
  S.append(s[i])
if '#' in s:
  for i in range(N):
    if s[i]=='#':
      t=i
      break
else:
  t=N 
c0=0
for j in range(t,N):
  if s[j]=='.':
    c0+=1   
c1=0
for i in range(N-2,-1,-1):
  if S[i]=='#' and S[i+1]=='.':
    S[i]='.'
    c1+=1
print(min(c0,c1))

Submission status: Wrong Answer

Solution code:
N=int(input())
s=input()
S0=[]
S1=[]
for i in range(N):
  S0.append(s[i])
  S1.append(s[i])
c0=0
for i in range(N-1):
  if S0[i]=='#' and S0[i+1]=='.':
    S0[i+1]='#'
    c0+=1
c1=0
for i in range(N-2,-1,-1):
  if S1[i]=='#' and S1[i+1]=='.':
    S1[i]='.'
    c1+=1
print(min(c0,c1))

Submission status: Wrong Answer

Solution code:
N,S = open(0).read().split()
ans = 0
ls = []
cnt = 1
p = S[0]
for c in S[1:]:
  if p!=c:
    p = c
    ls.append(cnt)
    cnt = 1
  else:
    cnt += 1
ls.append(cnt)
if S[0]=='.':
  ls = ls[1:]
if len(ls)%2==1:
  ls = ls[:-1]
m = sum(ls[i] for i in range(0,len(ls),2))
n = sum(ls)-m
ans = min(n,m)
print(ans)

Submission status: Wrong Answer

Solution code:
N,S = open(0).read().split()
ans = 0
ls = []
cnt = 1
p = S[0]
for c in S[1:]:
  if p!=c:
    p = c
    ls.append(cnt)
    cnt = 1
  else:
    cnt += 1
ls.append(cnt)
if S[0]=='.':
  ls = ls[1:]
m = sum(ls[i] for i in range(0,len(ls),2))
n = sum(ls)-m
ans = min(n,m)
print(ans)

Submission status: Wrong Answer

Solution code:
N,S = open(0).read().split()
ans = 0
ls = []
cnt = 1
p = S[0]
for c in S[1:]:
  if p!=c:
    p = c
    ls.append(cnt)
    cnt = 1
  else:
    cnt += 1
ls.append(cnt)
m = sum(ls[i] for i in range(0,len(ls),2))
n = sum(ls)-m
ans = min(n,m)
print(ans)

Submission status: Wrong Answer

Solution code:
N,S = open(0).read().split()
ans = 0
ls = []
cnt = 1
p = S[0]
for c in S[1:]:
  if p!=c:
    p = c
    ls.append(cnt)
    cnt = 1
  else:
    cnt += 1
ls.append(cnt)
for n,m in zip(*[iter(ls)]*2):
  ans += min(n,m)
print(ans)

Submission status: Wrong Answer

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    S = input()
    # ans_w, ans_b = 0, 0

    # started = False
    # for s in S:
    #     if s == '#':
    #         started = True
    #     if started and s == '.':
    #         ans_w += 1
    # started = False
    # for i in range(N):
    #     s = S[N - 1 - i]
    #     if s == '.':
    #         started = True
    #     if started and s == '#':
    #         ans_b += 1
    # print(min(ans_w, ans_b))
    Ws, Bs = [], []
    c = S[0]
    l = 1
    for i in range(1, N):
        s = S[i]
        if c == s:
            l += 1
        else:
            if c == '.':
                Ws.append(l)
            else:
                Bs.append(l)
            l = 1
            c = s
    if c == '.':
        Ws.append(l)
    else:
        Bs.append(l)
    # print(Ws, Bs)
    len_ws = len(Ws)
    len_bs = len(Bs)
    if len_ws == 0 or len_bs == 0:
        print(0)
        return
    if len_ws == 1:
        if len_bs == 1:
            if S[0] == '.':
                print(0)
            else:
                print(min(Bs[0], Ws[0]))
        else:
            # len_bs == 2
            print(min(Ws[0], Bs[0]))
        return
    if len_bs == 1:
        # len_ws == 2
        print(min(Ws[1], Bs[1]))
        return

    # len_ws >= 2 and len_bs >= 2
    sum_w_s, sum_w_r, sum_b_s, sum_b_r = [0]*len_ws, [0]*len_ws,  [0]*len_bs, [0]*len_bs
    sum_w_s[0] = Ws[0]
    sum_w_r[0] = Ws[-1]
    sum_b_s[0] = Bs[0]
    sum_b_r[0] = Bs[-1]
    for i in range(1, len_ws):
        sum_w_s[i] = sum_w_s[i - 1] + Ws[i]
        sum_w_r[i] = sum_w_r[i - 1] + Ws[len_ws - 1 - i]
    for i in range(1, len_bs):
        sum_b_s[i] = sum_b_s[i - 1] + Bs[i]
        sum_b_r[i] = sum_b_r[i - 1] + Bs[len_bs - 1 - i]
    # print(sum_w_s, sum_w_r, sum_b_s, sum_b_r)
    ans = min(sum_w_s[-1], sum_b_s[-1])
    if S[0] == '#':
        for i in range(len_ws - 1):
            a = sum_b_s[i] + sum_w_r[len_ws - 2 - i]
            ans = min(a, ans)
    else:
        for i in range(len_ws - 1):
            a = sum_w_r[i] + sum_b_s[len_bs - 2 - i]
            ans = min(a, ans)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    S = input()
    # ans_w, ans_b = 0, 0

    # started = False
    # for s in S:
    #     if s == '#':
    #         started = True
    #     if started and s == '.':
    #         ans_w += 1
    # started = False
    # for i in range(N):
    #     s = S[N - 1 - i]
    #     if s == '.':
    #         started = True
    #     if started and s == '#':
    #         ans_b += 1
    # print(min(ans_w, ans_b))
    Ws, Bs = [], []
    c = S[0]
    l = 1
    for i in range(1, N):
        s = S[i]
        if c == s:
            l += 1
        else:
            if c == '.':
                Ws.append(l)
            else:
                Bs.append(l)
            l = 1
            c = s
    if c == '.':
        Ws.append(l)
    else:
        Bs.append(l)
    # print(Ws, Bs)
    len_ws = len(Ws)
    len_bs = len(Bs)
    if len_ws == 0 or len_bs == 0:
        print(0)
        return
    if len_ws == 1:
        if len_bs == 1:
            if S[0] == '.':
                print(0)
            else:
                print(min(Bs[0], Ws[0]))
        else:
            # len_bs == 2
            print(min(Ws[0], Bs[0]))
        return
    if len_bs == 1:
        # len_ws == 2
        print(min(Ws[1], Bs[1]))
        return

    # len_ws >= 2 and len_bs >= 2
    sum_w_s, sum_w_r, sum_b_s, sum_b_r = [0]*len_ws, [0]*len_ws,  [0]*len_bs, [0]*len_bs
    sum_w_s[0] = Ws[0]
    sum_w_r[0] = Ws[-1]
    sum_b_s[0] = Bs[0]
    sum_b_r[0] = Bs[-1]
    for i in range(1, len_ws):
        sum_w_s[i] = sum_w_s[i - 1] + Ws[i]
        sum_w_r[i] = sum_w_r[i - 1] + Ws[len_ws - 1 - i]
    for i in range(1, len_bs):
        sum_b_s[i] = sum_b_s[i - 1] + Bs[i]
        sum_b_r[i] = sum_b_r[i - 1] + Bs[len_bs - 1 - i]
    # print(sum_w_s, sum_w_r, sum_b_s, sum_b_r)
    ans = min(sum_w_s[-1], sum_b_s[-1])
    if S[0] == '#':
        for i in range(len_ws - 1):
            a = sum_b_s[i] + sum_w_r[len_ws - 1 - i]
            ans = min(a, ans)
    else:
        for i in range(len_ws - 1):
            a = sum_w_r[i] + sum_b_s[len_bs - 1 - i]
            ans = min(a, ans)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if '#' in S:
    left = S.index('#')
    print(S[left:].count('.'))
else:
    print(0)

Submission status: Wrong Answer

Solution code:
N=int(input())
S=input()
leftid=-1
rightid=-1
blcnt=0
whcnt=0
cnt=0
#"#."のパターンをなくす".."か".#"か"##"なら良い
#最終的には".*#*"(左に白、右に黒)となれば良い
"""
#黒に変更の場合
#初めて黒になった箇所を左から探し、そこより右で白い部分を黒に
for i in range(N):
    if S[i]=="#":
        leftid=i
        break
if leftid!=-1:
    for i in range(leftid,N,1):
        if S[i]==".":
            blcnt=blcnt+1
else:
    blcnt=0

#白に変更の場合
#初めて白になった部分を右から探し、そこより左で黒い部分を白に
for i in range(N):
    if S[N-1-i]==".":
        rightid=N-i-1
        break
if rightid!=-1:
    for i in range(rightid,-1,-1):
        print(i)
        if S[i]=="#":
            whcnt=whcnt+1
else:
    whcnt=0
print(blcnt,whcnt)
print(min(blcnt,whcnt))
"""
for i in range(N-1):
    if (S[i]=="#")and(S[i+1]=="."):
        cnt=cnt+1
print(cnt)

Submission status: Wrong Answer

Solution code:
N=int(input())
S=input()
leftid=-1
rightid=-1
blcnt=0
whcnt=0
#黒に変更
for i in range(N):
    if S[i]=="#":
        leftid=i
        break
for i in range(N):
    if S[N-1-i]==".":
        rightid=N-i-1
        break
if leftid!=-1:
    for i in range(leftid,N,1):
        if S[i]==".":
            blcnt=blcnt+1
else:
    blcnt=0
if rightid!=-1:
    for i in range(rightid,-1,-1):
        if S[i]=="#":
            whcnt=whcnt+1
else:
    whcnt=0
print(min(blcnt,whcnt))

Submission status: Wrong Answer

Solution code:
N=int(input())
S=input()
leftid=-1
cnt=0
for i in range(N):
    if S[i]=="#":
        leftid=i
        break
if leftid!=-1:
    for i in range(leftid,N,1):
        if S[i]==".":
            cnt=cnt+1
    print(cnt)
else:
    print("0")

Submission status: Wrong Answer

Solution code:
# 左端が黒 - > すべての黒を白にするかすべての白を黒にしなくちゃいけない（反転は2倍ひっくりかえさなければいけないぶん意味がない）
# 左端が白 -> 左端から連続する白を除いたところ以降の白を全て黒にするか、黒をすべて白にするか、あるいは左端を含む白をすべて黒にする
n = int(input())
s = input() + "髭"
arr = []
l, r = 0, 0
while l < n:
    while r < n + 1 and s[r] == s[l]:
        r += 1
    arr.append(r - l)
    l = r
ruiseki = [0]
for a in arr:
    ruiseki.append(ruiseki[-1] + a)
if s[0] == "#":
    h = 0
    if s[-2] == "#":
        h = arr[-1]
    else:
        h = arr[0] + arr[-1]
    ans = min(s.count("#") - h, s.count("."))
    print(ans)
else:
    h = 0
    if s[-2] == "#":
        h = arr[-1]
    ans = min(s[ruiseki[1]:].count("#") - h, s[ruiseki[1]:].count("."),
              s.count("."))
    print(ans)

Submission status: Wrong Answer

Solution code:
# 左端が黒 - > すべての黒を白にするかすべての白を黒にしなくちゃいけない（反転は2倍ひっくりかえさなければいけないぶん意味がない）
# 左端が白 -> 左端から連続する白を除いたところ以降の白を全て黒にするか、黒をすべて白にするか、あるいは左端を含む白をすべて黒にする
n = int(input())
s = input() + "髭"
arr = []
l, r = 0, 0
while l < n:
    while r < n + 1 and s[r] == s[l]:
        r += 1
    arr.append(r - l)
    l = r
ruiseki = [0]
for a in arr:
    ruiseki.append(ruiseki[-1] + a)
if s[0] == "#":
    h = 0
    if s[-2] == "#":
        h = arr[-1]
    ans = min(s.count("#") - h, s.count("."))
    print(ans)
else:
    h = 0
    if list(s[-2]) == "#":
        h = arr[-1]
    ans = min(s[ruiseki[1]:].count("#") - h, s[ruiseki[1]:].count("."),
              s.count("."))
    print(ans)

Submission status: Wrong Answer

Solution code:
# 左端が黒 - > すべての黒を白にするかすべての白を黒にしなくちゃいけない（反転は2倍ひっくりかえさなければいけないぶん意味がない）
# 左端が白 -> 左端から連続する白を除いたところ以降の白を全て黒にするか、黒をすべて白にするか、あるいは左端を含む白をすべて黒にする
n = int(input())
s = input() + "髭"
arr = []
l, r = 0, 0
while l < n:
    while r < n + 1 and s[r] == s[l]:
        r += 1
    arr.append(r - l)
    l = r
ruiseki = [0]
for a in arr:
    ruiseki.append(ruiseki[-1] + a)
if s[0] == "#":
    ans = min(s.count("#"), s.count("."))
    print(ans)
else:
    h = 0
    if list(s[-2]) == "#":
        h = arr[-1]
    ans = min(s[ruiseki[1]:].count("#") - h, s[ruiseki[1]:].count("."),
              s.count("."))
    print(ans)

Submission status: Wrong Answer

Solution code:
# 左端が黒 - > すべての黒を白にするかすべての白を黒にしなくちゃいけない（反転は2倍ひっくりかえさなければいけないぶん意味がない）
# 左端が白 -> 左端から連続する白を除いたところ以降の白を全て黒にするか、それに加えて連続する黒を除いた黒をすべて白にするか、あるいは左端を含む白をすべて黒にする
n = int(input())
s = input() + "髭"
arr = []
l, r = 0, 0
while l < n:
    while r < n + 1 and s[r] == s[l]:
        r += 1
    arr.append(r - l)
    l = r
ruiseki = [0]
for a in arr:
    ruiseki.append(ruiseki[-1] + a)
if s[0] == "#":
    ans = min(s.count("#"), s.count("."))
    print(ans)
else:
    ans = min(s[ruiseki[1]:].count("#") - (arr[-1] if s[-1] == "#" else 0),
              s[ruiseki[1]:].count("."), s.count("."))
    print(ans)

Submission status: Wrong Answer

Solution code:
# 左端が黒 - > すべての黒を白にするかすべての白を黒にしなくちゃいけない（反転は2倍ひっくりかえさなければいけないぶん意味がない）
# 左端が白 -> 左端から連続する白を除いたところ以降の白を全て黒にするか、それに加えて連続する黒を除いた黒をすべて白にするか、あるいは左端を含む白をすべて黒にする
n = int(input())
s = input() + "髭"
arr = []
l, r = 0, 0
while l < n:
    while r < n + 1 and s[r] == s[l]:
        r += 1
    arr.append(r - l)
    l = r
ruiseki = [0]
for a in arr:
    ruiseki.append(ruiseki[-1] + a)
if s[0] == "#":
    ans = min(s.count("#"), s.count("."))
    print(ans)
else:
    ans = min(s[ruiseki[1]:].count("#") - arr[-1] if s[-1] == "#" else 0,
              s[ruiseki[1]:].count("."), s.count("."))
    print(ans)

Submission status: Wrong Answer

Solution code:
# 左端が黒 - > すべての黒を白にするかすべての白を黒にしなくちゃいけない（反転は2倍ひっくりかえさなければいけないぶん意味がない）
# 左端が白 -> 左端から連続する白を除いたところ以降の白を全て黒にするか、黒をすべて白にするか、あるいは左端を含む白をすべて黒にする
n = int(input())
s = input() + "髭"
arr = []
l, r = 0, 0
while l < n:
    while r < n + 1 and s[r] == s[l]:
        r += 1
    arr.append(r - l)
    l = r
ruiseki = [0]
for a in arr:
    ruiseki.append(ruiseki[-1] + a)
if s[0] == "#":
    ans = min(s.count("#"), s.count("."))
    print(ans)
else:
    ans = min(s[ruiseki[1]:].count("#"), s[ruiseki[1]:].count("."),
              s.count("."))
    print(ans)

Submission status: Wrong Answer

Solution code:
# 左端が黒 - > すべての黒を白にするかすべての白を黒にしなくちゃいけない（反転は2倍ひっくりかえさなければいけないぶん意味がない）
# 左端が白 -> 左端から連続する白を除いたところ以降の白を全て黒にするか、黒をすべて白にするか、あるいは左端を含む白をすべて黒にする
n = int(input())
s = input() + "髭"
arr = []
l, r = 0, 0
while l < n:
    while r < n + 1 and s[r] == s[l]:
        r += 1
    arr.append(r - l)
    l = r
ruiseki = [0]
for a in arr:
    ruiseki.append(ruiseki[-1] + a)
if s[0] == "#":
    ans = min(s.count("#"), s.count("."))
    print(ans)
else:
    ans = min(s[ruiseki[1]:].count("#"), s.count("."))
    print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

right_white = N
for i in range(N - 1, -1, -1):
    if S[i] == ".":
        right_white = i
        break
left_black = 0
for i in range(N):
    if S[i] == "#":
        left_black = i
        break

print(
    min(
        S[left_black:].count("."),
        S[:right_white].count("#"),
        S.count("."),
        S.count("#"),
    )
)

Submission status: Wrong Answer

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

right_white = N
for i in range(N - 1, -1, -1):
    if S[i] == ".":
        right_white = i
        break
left_black = 0
for i in range(N):
    if S[i] == "#":
        left_black = i
        break

print(min(S[left_black:].count("."), S[:right_white].count("#")))

Submission status: Wrong Answer

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

    bstone = [0]*n
    wstone = [0]*n

    bstone[0] = 1 if s[0]=='#' else 0
    wstone[-1] = 1 if s[-1]=='.' else 0

    for i in range(n-1):
        bstone[i+1] = bstone[i]
        if s[i]=='#':
            bstone[i+1]+=1
    
    for i in range(n-1)[::-1]:
        wstone[i] = wstone[i+1]
        if s[i]=='.':
            wstone[i]+=1

    bstone.insert(0,0)
    wstone.append(0)

    ans = []
    for i in range(n):
        t = bstone[i]+wstone[i+1]
        ans.append(t)
    print(min(ans))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

aw=0
ab=0
for i in range(len(l)):
    aw+=l[i][0]
    ab+=l[i][1]
ans=min(aw,ab)
#print(l)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
t_min = min(S.count("#"), S.count("."))
S_first = [S[0]]
S_second = list(S[1:])
for i in range(1,N):
    S_first.append(S[i])
    del S_second[0]
    t = 0
    t += S_first.count("#")
    t += S_second.count(".")
    t_min = min(t_min, t)
print(t_min)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
# 矛盾点の前にある黒石の数と、後ろにある白石の数の小さい方＋１をカウント
ans = 0
black_cnt = 0
prev = ""
L = R = 0
while L < n:
    if prev+s[L] == "#.":
        white_cnt = 0
        while R < n-1:
            R += 1
            if s[R] != '.':
                break
            white_cnt += 1
        ans += min(black_cnt, white_cnt)+1
        L = R = R+1
        continue
    if s[L] == "#":
        black_cnt += 1
    else:
        black_cnt = 0
    prev = s[L]
    L += 1
    R += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
s = [i for i in input()]
cnt_s, cnt_d = [0]*n, [0]*n
for i in range(n):
    if s[i] == "#":
        cnt_s[i] = 1
    else:
        cnt_d[i] = 1
cnt_s, cnt_d = list(itertools.accumulate(cnt_s)), list(itertools.accumulate(cnt_d))
ans = n
cnt_d_l = cnt_d[-1]
for i in range(n):
    ans = min(ans, cnt_s[i] + (cnt_d_l - cnt_d[i]))
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
s = [i for i in input()]
if s[0] == ".":
    for i in range(n):
        if s[i] == "#":
            s = s[i:]
            break
    if len(s) == n:
        print(0)
        exit()
gr = itertools.groupby(s)
ans = 0
cnt_s = 0
for k, g in gr:
    if k == "#":
        cnt_s = len(list(g))
    elif k == ".":
        ans += min(cnt_s, len(list(g)))
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
s = [i for i in input()]
ans = 0
gr = itertools.groupby(s)
cnt = 0
for k, g in gr:
    if k == ".":
        if cnt == 0:
            cnt += 1
            continue
        else:
            ans += len(list(g))
    cnt += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
s = [i for i in input()]
ans = 0
gr = itertools.groupby(s)
cnt = 0
for k, g in gr:
    if k == ".":
        if cnt == 0:
            cnt += 1
            continue
        else:
            ans += 1
    cnt += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
s = [i for i in input()]
ans = 0
gr = itertools.groupby(s)
cnt = 0
for k, g in gr:
    if cnt == 0 and k == ".": continue
    if k == ".": ans += 1
    cnt += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
c=str(input())
#c=c[::-1]
#print(c)
c_count=[]
count1=0
count2=0


for i in range(n):
    if c[i]=='.':
        if count2!=0:
            c_count.append(count2)
            count2=0
        count1+=1
    else:
        if count1!=0:
            c_count.append(count1)
            count1=0
        count2+=1
        
c_count.append(max(count1,count2))

#print(c_count)
ans=0
for i in range(0,len(c_count)-1,2):
    ans+=min(c_count[i],c_count[i+1])
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input())
b_c,h_c = s.count('#'),s.count('.')
ans = n
b_r,h_r = 0,0
for i in range(n-1):
    if s[i]=='#':b_r+=1
    else:h_r+=1
    if s[i]!=s[i+1]:
        ans = min(ans,b_r+h_c-h_r)
if s[-1]=='#':b_r+=1
else:h_r+=1
ans = min(ans,b_r+h_c-h_r)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

sharp_list = [0 for i in range(n)]
dot_list = [0 for i in range(n)]
for i in range(n):
    if s[i] == '#':
        sharp_list[i] = sharp_list[i-1] + 1
    else:
        sharp_list[i] = 0
for i in range(n-1,-1,-1):
    if s[i] == '.':
        dot_list[i] = dot_list[(i+1)%n] + 1
    else:
        dot_list[i] = 0
ans = 0
for i in range(n-1):
    if s[i:i+2] == "#.":
        ans += 1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
"#."
sharp = 0
dot = 0
sharp_list = [0 for i in range(n)]
dot_list = [0 for i in range(n)]
for i in range(n):
    if s[i] == '#':
        sharp_list[i] = sharp_list[i-1] + 1
    else:
        sharp_list[i] = 0
for i in range(n-1,-1,-1):
    if s[i] == '.':
        dot_list[i] = dot_list[(i+1)%n] + 1
    else:
        dot_list[i] = 0
ans = 0
for i in range(n-1):
    if s[i:i+2] == "#.":
        ans += min(sharp_list[i],dot_list[i+1])
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input())
s_r = list(reversed(s))
b_c = 0
h_c = 0
ans = 0
for i in range(n-1):
    if s[i]!=s[i+1]:
        if (b_c==1 and h_c==0) or (h_c==1 and b_c==0):
            ans += 1
            s[i+1] = s[i] 
        else:
            if s[i]=='#':
                b_c = 1
                h_c = 0
            else:
                b_c = 0
                h_c = 1
    else:
        if s[i]=='#':
            b_c += 1
        else:
            h_c += 1 
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input())
s_r = list(reversed(s))
b_c = 0
h_c = 0
ans = 0
for i in range(n-1):
    if s[i]!=s[i+1]:
        if b_c==1 or h_c==1:
            ans += 1
            s[i+1] = s[i] 
        else:
            if s[i]=='#':
                b_c = 1
                h_c = 0
            else:
                b_c = 0
                h_c = 1
    else:
        if s[i]=='#':
            b_c += 1
        else:
            h_c += 1 
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input())
s_r = list(reversed(s))
b_c = 0
h_c = 0
ans = 0
for i in range(n-1):
    if s[i]!=s[i+1] and (b_c==1 or h_c==1):
        ans += 1
        s[i+1] = s[i] 
    else:
        if s[i]=='#':
            b_c = 1
            h_c = 0
        else:
            b_c = 0
            h_c = 1
ans2 = 0
for i in range(n-1):
    if s_r[i]!=s_r[i+1] and (b_c==1 or h_c==1):
        ans2 += 1
        s_r[i+1] = s_r[i] 
    else:
        if s_r[i]=='#':
            b_c = 1
            h_c = 0
        else:
            b_c = 0
            h_c = 1
print(min(ans,ans2))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input())
b_c = 0
h_c = 0
ans = 0
for i in range(n-1):
    if s[i]!=s[i+1] and (b_c==1 or h_c==1):
        ans += 1
        s[i+1] = s[i] 
    else:
        if s[i]=='#':
            b_c = 1
            h_c = 0
        else:
            b_c = 0
            h_c = 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

N = ii()
S = input()

w_cnt = 0
b_cnt = 0

for i in S:
    if i == '.':
        w_cnt += 1
    else:
        break

for i in S[::-1]:
    if i == '#':
        b_cnt += 1
    else:
        break

w = b = 0

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

#print(w_cnt, b_cnt, w, b)
print(min(w, b))