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:
import sys

sys.setrecursionlimit(10000000)


def check_and_back_or_go_to(li, ind, N, counter, white_charge, black_charge):
    back = True
    # print('see {} {} and {} counter{}'.format(ind, li[ind - 1], li[ind], counter))
    if ind == 0:
        return counter

    # access
    if li[ind - 1] == '#' and li[ind] == '.':
        print('add')
        c = check_and_back_or_go_to(li, ind - 1, N, counter + 1, white_charge, black_charge)
        return c
    elif li[ind - 1] == '.' and li[ind] == '.':
        return check_and_back_or_go_to(li, ind - 1, N, counter, white_charge + 1, black_charge)

    elif li[ind - 1] == '#' and li[ind] == '#':
        return check_and_back_or_go_to(li, ind - 1, N, counter, white_charge , black_charge + 1)
    elif li[ind - 1] == '.' and li[ind] == '#':
        adder = min([white_charge, black_charge])
        # print('adder {} WP{}'.format(adder,white_charge))
        return check_and_back_or_go_to(li, ind - 1, N, counter + adder, 0, 0)
    else:
        c = check_and_back_or_go_to(li, ind - 1, N, counter, white_charge, black_charge)
        return c


N = int(input())
S = list(input())
r = range(N)
if N == 1:
    print(0)
    exit(0)
bsk = False
key = -1
for i in range(len(S)):
    if not bsk:
        if S[i] == '#':
            bsk = True
            key = i
    else:
        if S[i] != '#':
            bsk = False

if bsk and key == 0:
    print(0)
    exit(0)

if bsk:
    # print('bsk occured')
    # print([S[i] for i in range(key)])
    count = check_and_back_or_go_to([S[i] for i in range(key)], key - 1, N, 0, 0, 0)
    print(min([count, key - count]))
else:
    count = check_and_back_or_go_to(S, len(S) - 1, N, 0, 0, 0)
    print(min([count, N - count]))

Submission status: Wrong Answer

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

count0=0
count1=0
count2=0
count3=0

for j in range(N):
    if S[j]=='.':
        count0+=1
    else:
        break

for k in (reversed(S)):
    if k=='#':
        count3+=1
    else:
        break

for i in range(count0,N):
    if S[i]=='#':
        count1+=1
    elif S[i]=='.':
        count2+=1

if count1==0 or count2==0:
    print('0')
else:
    if count1-count3<count2:
        print(count1-count3)
    elif count1>count2:
        print(count2)
    else:
        print(count1)

Submission status: Wrong Answer

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

post = ''
ans=0
data=[]

left=[]
right=[]

tmp=0
for i in range(n):
    if s[i]=='#':
        tmp+=1
    elif tmp!= 0:
        left.append(tmp)
        tmp=0


tmp=0
for i in range(n):
    if s[n-i-1]=='.':
        tmp+=1
    if n-i-1-1 >=0 and s[n-i-1-1]=="#" and tmp!=0:
        right.append(tmp)
        tmp=0

right.reverse()
# print(s)
# print(left)
# print(right)

for i in range(len(left)):
    if i == len(left)-1:
        ans+=min(left[i],right[i])
        break
    l = left[i]
    r = right[i]
    if l >= r:
        ans+=r
        left[i+1]=l+r+left[i+1]
    else:
        ans+=l

if '#' not in s:
    ans = 0

if '.' not in s:
    ans=0
print(ans)

Submission status: Wrong Answer

Solution code:
a=int(input())
b=input()
count=0
ten_count=[0,0]
if b[0]=="#": 
    ten_count[0]+=1
for i in range(1,a):
    if b[i]=="#" and b[i-1]=="#":
        ten_count[0]+=1
    elif b[i-1]=="#" and b[i]==".":
        ten_count[1]+=1
        count=min(ten_count[0],ten_count[1])
    elif b[i-1]=="." and b[i]=="." :
        ten_count[1]+=1
        count=min(ten_count[0],ten_count[1])
    elif (b[i-1]=="." and b[i]=="#") :
        count=min(ten_count[0], ten_count[1])
        ten_count[0]+=1
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
s += "-"
white = [0]
black = [0]
white_acc = 0
black_acc = 0
counter = 0
for i in range(n + 1):
    if s[i] == s[i - 1]:
        pass

    elif s[i - 1] == "#":
        black_acc += counter
        black += [black_acc]
        counter = 0
    elif s[i - 1] == ".":
        white_acc += counter
        white += [white_acc]
        counter = 0
    counter += 1
if len(black) + len(white) <= 3:
    print(0)
    exit()

last_white = white[-1]
min_acc = 2 * 10 ** 5
if s[0] == "#":
    for i in range(len(white) - 1):
        min_acc = min(min_acc, last_white - white[i] + black[i])

else:
    black += [0]
    for i in range(len(white) - 1):
        min_acc = min(min_acc, last_white - white[i] + black[i - 1])
print(min(min_acc, last_white, black[-1]))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
start = s[0]
pattern = []

prev = ''
b = 0
w = 0
for i in s:
    if (i == '#' and prev == '#') or (i == '#' and prev ==''):
        b += 1
    elif (i == '.' and prev == '.') or (i == '.' and prev == ''):
        w += 1
    elif i == '#':
        b += 1
        pattern.append(w)
        w = 0
    else:
        w += 1
        pattern.append(b)
        b = 0
    prev = i
    
if prev == '#':
    pattern.append(b)
else:
    pattern.append(w)

print(pattern)
totalb = 0
totalw = 0
for i in range(len(pattern)):
    if start == '.':
        if i % 2 == 0:
            totalw += pattern[i]
        else:
            totalb += pattern[i]
    else:
        if i % 2 == 0:
            totalb += pattern[i]
        else:
            totalw += pattern[i]
        
if len(pattern) == 1 or (len(pattern) == 2 and start == '.'):
    print(0)
else:
    if start == '.':
        print(min(totalw - pattern[0], totalb))
    else:
        print(min(totalw, totalb))

Submission status: Wrong Answer

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

def counting(seq, s):
  return seq.count(s)

base_count = S.count("#.")

S = S.replace("#.", "")

idx_list = [0, len(S)]
for i in range(len(S)-1):
  if S[i] == "." and S[i+1] == "#":
    idx_list.append(i)

count_list = []
for i in idx_list:
  count_list.append(counting(S[:i+1], "#") + counting(S[i+1:], "."))
count_list.append(counting(S, "#")+0)
print(min(count_list) + base_count)

Submission status: Wrong Answer

Solution code:
import numpy as np
n=2
a=[input() for i in range(n)]
N=int(a[0])
S=list(a[1])
 
kazuXD=0
flg=0
kazu=0
geri=0
for i in range(N):
  if S[i]=="#":
    geri+=1
for i in range(N):
  if flg==0:
    if S[i]==".":
      kazu+=1
    else:
      flg=1
flg=0
for i in range(N):
  if i>=kazu:
    if S[i]==".":
      kazuXD+=1

if geri>kazuXD:
  print(kazuXD)
else:
  print(geri)

Submission status: Wrong Answer

Solution code:
N=int(input())
S=input()
R=S[::-1]
cb=0
B=[]
sb="$"
for s in S:
    if s=='.' and sb=='#':
        B.append(cb)
    if s=='#':
        cb+=1
    sb=s
cw=0
W=[]
sb="$"
for s in R:
    if s=="#" and sb=='.':
            W.append(cw)
    if s=='.':
        cw+=1
    sb=s

W=W[::-1]

result=N
for b, w in zip(B, W):
    #print(b, w)
    result=min(result, b+w-1)
if result==N:
    result = 0
print(result)

Submission status: Wrong Answer

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

def calc():
    min_cost = n
    left_cost = 0
    right_cost = s.count('.')

    if right_cost == 0:
        return(0)

    for i in range(n):
        if s[i] == '#':
            left_cost += 1
        else:
            right_cost -= 1
        cost = left_cost + right_cost
        if cost < min_cost:
            min_cost = cost

    return(min_cost)

print(calc())

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
count = 0
for i in s:
  if i == ["#"]:
    if s[(s.index(i))+1] == ["."]:
      count += 1
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
# 全部白か黒

if s[-1] == ".":
    s += "#"

li = []
li2 = []
tmp = 0
tmp2 = 0
for i in s:
    if i == "#":
        tmp += 1
    else:
        li.append(tmp)
        tmp = 0

for i in s:
    if i == ".":
        tmp2 += 1
    else:
        li2.append(tmp2)
        tmp2 = 0

#print(list(filter(lambda a: a != 0, li)))
#print(list(filter(lambda a: a != 0, li2)))
#print(li)
#print(li2)
li = list(filter(lambda a: a != 0, li))
li2 = list(filter(lambda a: a != 0, li2))
#print(li)
#print(li2)

try:
    a = li[0] + sum(li2[1:])
except:
    a = 100000000000000
b = sum(li)# 全部白
c = sum(li2)# 全部黒
print(min(a, b, c))

Submission status: Wrong Answer

Solution code:
def MI(): return map(int, input().split())
def II(): return int(input())
def IS(): return input()
def LI(): return list(map(int, input().split()))


n = II()
s = list(IS())
s2 = s[::-1]
count = 0
count2 = 0

if len(s) == 1:
    print(0)
    exit()
for i in range(len(s)):
    if i == 0 and s[i] == '#' and s[i+1] == '.':
        s[i] = '.'
        count += 1
    elif i == len(s)-1:
        break
    elif s[i-1] == '.' and s[i] == '#' and s[i+1] == '.':
        count += 1
        s[i] = '.'
    elif s[i-1] == '#' and s[i] == '#' and s[i+1] == '.':
        count += 1
        s[i+1] = '#'

for i in range(len(s)):
    if i == 0 and s2[i] == '.' and s2[i+1] == '#':
        s2[i] = '#'
        count2 += 1
    elif i == len(s)-1:
        break
    elif s2[i-1] == '#' and s2[i] == '.' and s2[i+1] == '#':
        count2 += 1
        s[i] = '#'
    elif s2[i-1] == '.' and s2[i] == '.' and s2[i+1] == '#':
        count2 += 1
        s2[i+1] = '.'

print(min(count,count2))

Submission status: Wrong Answer

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

flag = False
count_w = 0
count_b = 0

count = 0
S_map = []
for i in S:
    if(i=="."):
        count += 1
        S_map.append(count)
    else:
        S_map.append(count)
ans = []
for i in range(N):
    if(i == 0 or S[i-1] == "." and S[i] == "#"):
        if(i != 0):
            left_white = S_map[i]
            left_black = i - left_white
        else:
            left_white = 0
            left_black = 0
        if(i != N-1):
            right_white = S_map[-1] - S_map[i]
            right_black = (N-i-1) - right_white
        else:
            right_white = 0
            right_black = 0
        ans.append(min(left_white+right_white, left_black+right_black, left_black+right_white))
print(min(ans))

Submission status: Wrong Answer

Solution code:
def indexOfLeftWhite(S) :
    for i in range(len(S)):
        if (S[i] == ".") :
            return i
    return -1

def indexOfLeftBlack(S) :
    for i in range(len(S)):
        if (S[i] == "#") :
            return i
    return -1

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

counter = 0

if (S[-2:] == "#.") :
    S = S[:-1] + "#"
    counter += 1
"""
else:
    for i in range(1,len(S))[::-1] :
        if (S[i-1:i+2] == "#.#") :
            S = S[:i-1] + "###" + S[i+2:]
            counter += 1
        elif (S[i-1:i+2] == "#..") :
            S = S[:i-1] + "..." + S[i+2:]
            counter += 1
"""

for i in range(5) :
    tmp = S.replace("#..", "...") 
    L = tmp.replace("#.#", "###")

for i in range(len(S)) :
    if (S[i] != L[i]) :
        counter += 1

print(counter)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
S = list(input())

BLACK = '#'
WHITE = '.'

count = 0
for idx in range(N - 1):
    if S[idx] == BLACK and S[idx + 1] == WHITE:
        if idx > 0:
            if S[idx - 1] == WHITE:
                S[idx] = WHITE
            else:
                S[idx + 1] = BLACK
        else:
            S[idx] = WHITE
        count += 1

# print(S)

for idx in range(N - 1):
    if S[idx] == BLACK and S[idx + 1] == WHITE:
        if idx > 0:
            if S[idx - 1] == WHITE:
                S[idx] = WHITE
            else:
                S[idx + 1] = BLACK
        else:
            S[idx] = WHITE
        count += 1
# print(S)

print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=list(input())
li=[]
if s[0]=='.':
  a=0
  s.insert(0,"あ")
  for i in range (1,n+1):
    if s[i]==s[i-1]:
      a=a+1
    else:
      li.append(a)
      a=1
  li.append(a)
else:
  a=1
  for i in range (0,n-1):
    if s[i+1]==s[i]:
      a=a+1
    else:
      li.append(a)
      a=1
  if s[n-2]==s[n-1]:
    a=a+1
    li.append(a)
  else:
    li.append(a)
if s[0]=="あ":
  s.pop(0)
if s[n-1]==".":
  li.append(0)
len_li=len(li)
import math
t=math.floor(len_li/2)+1
l=[]
m=0
for j in range (0,t-1):
  m=m+li[2*j+1]
x=0
y=0
for k in range (0,t):
  l.append(m+y-x)
  y=y+li[2*k]
  if k!=t-1:
    x=x+li[2*k+1]
l.sort()
print(l[0])

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
siro = S.count(".")
kuro = S.count("#")
change_num = 0
for i in range(N-1):
	if S[i] == "#" and S[i+1]==".":
		if siro > kuro:
			S[i+1] == "#"
		else:
			S[i] == "."
		change_num = change_num + 1
print(change_num)

Submission status: Wrong Answer

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

s = list(S)

for i in range(N-1, -1, -1):
    if s[i] == "#":
        s[i] = ""
    else:
        break
    
for i in range(N):
    if s[i] == ".":
        s[i] = ""
    else:
        break

print(min(s.count("#"), s.count(".")))

Submission status: Wrong Answer

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

min = n
i = -1
for j in range(n):
  j = s.find(".#", i + 1)
  if j < 0:
    break
  a = s[:j+1].count("#") + s[j+1:].count(".")
  if a < min:
    min = a
  i = j
print(min)

Submission status: Wrong Answer

Solution code:
import sys

def solve(N, S):

    ans = 0
    prev = S[0]
    for i in xrange(len(S)-1):
        if prev == "#" and S[i+1] == ".":
            ans += 1
            prev = "."
        else:
            prev = S[i+1]
    return ans

if __name__ == '__main__':

    N = sys.stdin.readline().strip()
    S = sys.stdin.readline().strip()
    ans = solve(int(N), S)
    print ans

Submission status: Wrong Answer

Solution code:
import copy

n=int(input())
stemp=input()
s=[ i for i in stemp]
s.append("NG")

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

    elif s[i]=="#":
        f=1


print(ans)

Submission status: Wrong Answer

Solution code:
import sys

N = int(sys.stdin.readline().strip())
S = sys.stdin.readline().strip()

first = S[0]
end = S[-1]
array = []
tmp = first

for i, s in enumerate(S):
    if s != tmp:
        array.append(i-1)
        tmp = s

if N-1 not in array:
    array.append(N-1)

nn = len(array)
res = 0

array = list(reversed(array))
#print(array)

if end == ".":
    for i, index in enumerate(array):
        if i % 2 != 0 or i == len(array)-1:
            continue
        if index - array[i+1]  > array[i+1] + 1:
            #print(i, "#")
            res += (array[i+1] + 1)
            break
        else:
            #print(i, "##")
            #print(array[i-1], index)
            res += (index - array[i+1])
else:
    for i, index in enumerate(array):
        if i % 2 == 0 or i == len(array)-1:
            continue
        if index - array[i+1]  > array[i+1] + 1:
            res += (array[i+1] + 1)
            break
        else:
            res += (index - array[i+1])
    
print(res)

Submission status: Wrong Answer

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

if s.count('.') == n:
    print(count)
else:
    l = []
    l_count = int(s.count('#'))
    l_index = int(s.index('#'))
    for i in range(l_index, n):
        if s[i] == '#':
            l.append(i)
#    print(l)
    if l_index + len(l) == n:
        print(count)
    elif l_index > 0:
        count += n - l_index - len(l)
        print(count)
    else:
        count += n - len(l)
        print(count)

Submission status: Wrong Answer

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

ans = 2*10**5
c_b = 0
c_w = 0
for i in range(N):
    if S[i] == '#':
        c_b += 1
    else:
        c_w += 1

ans = min(ans, c_b, c_w)

nums = []
ns = 0
if S[0] == '#':
    nums.append(0)
    ns = 1

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

if len(nums) < 3:
    print(0)
    quit()

count = 0
for i in range(0, len(nums), 2):
    if i != 0:
        if nums[i-1] != 0:
            count += nums[i-1]
ans = min(ans, count)

count = 0
for i in range(1, len(nums), 2):
    if nums[i-1] != 0:
        count += nums[i-1]
if (len(nums)-1) % 2 != 1:
    count += nums[-1]

print(min(ans, count))

Submission status: Wrong Answer

Solution code:
size = int(input())
stone= str(input())
cnt=0
cnt2=0
cnt3=0
cnt4=0
for i in range(size):
    if(stone[i]=="."):
        cnt+=1
    else:
        cnt4+=1
        break
for i in range(size):
    if(stone[i]=="."):
        cnt2+=1
    else:
        cnt3+=1
        
if(stone[0]=="#"):
    if(cnt4>cnt2):
        print(cnt2)
    else:
        print(cnt4)
    
else:
    if(cnt2-cnt<=cnt3):
        print(cnt2-cnt)
    else:
        print(cnt3)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(input())
c = 0
e = []
st = a[N-1]
f = [st,1]
for i in range(2,N+1):
    if a[N-i] == st:
        f[1] += 1
    else:
        e.append(f)
        st = a[N-i]
        f = [st,1]
e.append(f)
for i in range(len(e) - 1):
    if e[i][0] == ".":
        c += e[i][1]
print(min(c,N-c))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
count1 = 0
count2 = 0
count = 0
counter = []
flag = False

for i in range(N):
    if S[i] == '#':
        flag = True
    if S[i] == '.' and flag == True:
        count2 += 1

for i in range(N):
    if S[i] == '.':
        count1 += count
        count = 0
    else:
        count += 1

print(min(count1, count2))

Submission status: Wrong Answer

Solution code:
n = int(input())
 
lines = list(input())
 
count_stone = list()
stone = lines[0]
count=0
for i in range(len(lines)):
    if lines[i] == stone:
        count += 1
    else:
        count_stone.append([stone,count])
        stone = lines[i]
        count = 1
    if i == len(lines)-1:
        count_stone.append([stone,count])
    
# print(count_stone)
ans = list()
for i in range(len(count_stone)):
    total = 0
    for j in range(1,len(count_stone)):
        if j <= i:
            if count_stone[j][0] == '.':
                total += count_stone[j][1]
        else:
            if count_stone[j][0] == '#':
                total += count_stone[j][1]
    ans.append(total)
print(min(ans))

Submission status: Wrong Answer

Solution code:
import sys

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

bcnt = 0
wcnt = 0
bst = -1
lastlen = 0

for i in range(len(s)):
    tmp = s[i:i + 1]
    if "#" in tmp:
        bcnt += 1
        if bst == -1:
            bst = i
        lastlen += 1
    elif "." in tmp:
        if bst >= 0:
            wcnt += 1
        lastlen = 0

print(min(wcnt, bcnt-lastlen))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = str(input())
 
n1 = S.count(".")
n2 = S.count("#")
s1 = S.split("#")
print(s1)
n3 = N
for i in range (N):
    tco = S[:i].count("#") + S[i:].count(".")
    if n3 > tco:
        n3 = tco

print(min(n1,n2,n3))

Submission status: Wrong Answer

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

i = S.find('#')
if i < 0:
    print(0)
    sys.exit()

a = 0
c = S.count('.')
Min = 200000


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

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(input())
cnta = 0
la = l.copy()
cntb= 0
lb = l.copy()
for i in range(1, n):
    if (la[i] == ".")&(la[i-1] == "#"):
        la[i] = "#"
        cnta += 1

for i in range(0, n-1):
    if (lb[i] == "#")&(lb[i+1] == "."):
        lb[i] = "."
        cntb += 1

print(min(cnta, cntb))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
 
fb = False
lw = -1
awc = 0
abc = 0
fbrwc = 0
lwlbc = 0
for i in range(N):
  if S[i] == '.':
    awc += 1
    lw = i
    if fb:
      fbrwc += 1
  if S[i] == '#':
    abc += 1
    if fb:
      fb = True
for i in range(lw):
  if S[i] == '#':
    lwlbc += 1
  
count = []
count.append(awc)
count.append(abc)
count.append(lwlbc)
count.append(fbrwc)
print(min(count))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
S = list(input())

s_count = 0
d_count = 0
SS = ''
for i in range(N):
    if S[i:i+2]==['#', '.']:
        SS = S[i:N]
        break
for i in range(len(SS)):
    if SS[i]=='#': s_count+=1
    elif SS[i]=='.': d_count+=1

print(min(s_count, d_count))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = str(input())
s += "#"
b = 0
w = 0
ans = 0
for s_ in s:
  if s_ == "#":
    if w == 0:
      b = b + 1
    else:
      if b == w:
        ans = ans + b
      else:
        ans = ans + min(b,w)
      b = 1
      w = 0
  if s_ == ".":
    if b > 0:
      w = w + 1

print(ans)

Submission status: Wrong Answer

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

import sys

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        lines.append(input())
        lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]


    n = int(lines[0])
    s = lines[1]

    return (n, s)


def solve(n, s):
    
    ss = []
    count_w = 1
    count_b = -1
    before = s[0]
    for i, c in enumerate(s[1:]):
        if before == c:
            if c == ".":
                count_w = count_w + 1
            else:
                count_b = count_b - 1
        else:
            if before == ".":
                ss.append(count_w)
                count_w = 1
            else:
                ss.append(count_b)
                count_b = -1
        before = c

    if before == ".":
        ss.append(count_w)
    else:
        ss.append(count_b)

    if debug:
        log("ss=%s" % ss)


    if ss[0] > 0:
       ss = ss[1:]
    
    count_w = sum([e for e in ss if e > 0])
    count_b = sum([e for e in ss if e < 0])

    result = min(count_w, abs(count_b))
    
    return result


def main():
    # 出力

    print("%s" % solve(*parse_input()))

if __name__ == '__main__':

    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
S=input()
c=0
d=0
switch=0
for i in range(N):
    if S[i]=='#' and switch==0:
        switch=1
    elif switch==1 and S[i]=='.':
        c=S[i-1:].count('.')
        break
S=S[::-1]
for i in range(N):
    if S[i]=='.' and switch==0:
        switch=1
    elif switch==1 and S[i]=='#':
        d=S[i-1:].count('#')
        break
print(min(c,d))

Submission status: Wrong Answer

Solution code:
import re
n = int(input())
s = input()
if not '#' in s:
    print(0)
else:
    s = '#' + s.split('#', 1)[-1]
    ptn = re.search(re.compile('\.#+?$'), s)
    if ptn:
        s = s[:ptn.end()]
    print(min(s.count('.'), s.count('#')))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
ans = 0
replace_point = -1
while 0 == 0:
    replace_point = S.find("#.")
    right_replace_point = replace_point
    if replace_point == -1:
        break
    left_rep = 1
    right_rep = 1
    if replace_point != 0:
        while 1 == 1:
        # 左を見に行く
            if S[replace_point-1:replace_point] == "#":
                left_rep += 1
                replace_point -= 1
            else:
                break
    if right_replace_point != len(S):
        while 1 == 1:
        # 右を見に行く
            if S[right_replace_point+2:right_replace_point+3] == ".":
                right_rep += 1
                right_replace_point += 1
            else:
                break
    ans += min(left_rep, right_rep)
    S = S[replace_point+2:]
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
raw = input()
start_idx = 0
count = 0
 
for s in raw:
    if s == '#':
        break
    start_idx = start_idx + 1
 
tmpb = 1
tmpw = 1
flg = '#'
reset = False
cont = False
for i in range(start_idx, n):
    if reset:
        tmpb = 1
        tmpw = 1
        flg = '#'
        reset = False
        cont = False
    if flg == '#':
        if raw[i] == flg:
            if cont == False:
                cont = True
            else:
                tmpb = tmpb + 1
        else:
            flg = '.'
    else:
        if raw[i] == flg:
            if cont == False:
                cont = True
            else:
                tmpw = tmpw + 1
        else:
            reset = True
            if tmpb >= tmpw:
                count = count + tmpw
            else:
                count = count + tmpb
 
if raw[-1] == '.': 
    if tmpb >= tmpw:
        count = count + tmpw
    else:
        count = count + tmpb
 
 
print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())-1;S=input();f=[];flag=0;l=0;
for i in range(N):
 if S[i] == "#" and i != N:
  if S[i+1] == ".":
   while (1):
    if S[i-l] != "#": #or (l>0 and i == 0):
     f.append(abs(l));l=0;break
    l+=1;
   while (1):
    if i+l != N:
     if S[i+l+1] != ".":
      f.append(l);l=0;break
     l+=1
    else:
     f.append(l);l=0;break
   flag += min(f);f=[];
print(flag)

Submission status: Wrong Answer

Solution code:
#d=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
#import numpy as np
#import math
#n=int(input())
#li=input().split()
#S=input()
#import fractions
#lis=list(map(int,input().split()))

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

count=0 #black
flag=-1 #first black
for i in range(n):
    if s[i]=="#" and flag==-1:
        flag=i
        break
a="."*i+"#"*(n-i)
b="."*n
c="#"*n
aa,bb,cc=0,0,0
for i in range(n):
    if a[i]!=s[i]:
        aa+=1
for i in range(n):
    if b[i]!=s[i]:
        bb+=1
for i in range(n):
    if c[i]!=s[i]:
        cc+=1

ans=min(aa,bb,cc)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
start = S.find("#")
if start == -1:
    print("0")
else:
    di = {".":"0","#":"1"}
    S_bin = int("0b"+S[start:].translate(str.maketrans(di)), 0)
    a = int("0b"+"1"*int(N-start) ,0)
    ans1 = min(bin(a^S_bin).count("1"),bin(a^S_bin).count("0"))
    ans4 = min(bin(int("0b"+"0"*int(N-start) ,0)^S_bin).count("1"),bin(int("0b"+"0"*int(N-start) ,0)^S_bin).count("0"))
    ans2 = min(bin(0^int("0b"+S.translate(str.maketrans(di)), 0)).count("1"),bin(0^int("0b"+S.translate(str.maketrans(di)), 0)).count("0"))
    ans3 = min(bin((int("0b"+"1"*int(N) ,0))^int("0b"+S.translate(str.maketrans(di)), 0)).count("1"),bin((int("0b"+"1"*int(N) ,0))^int("0b"+S.translate(str.maketrans(di)), 0)).count("0"))
    print(min(ans1,ans2,ans3,ans4))

Submission status: Wrong Answer

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

si=[]
k=[]
count = 0
flag = 0
flag2 = 0

for i in range(0,n):
    if flag == 0:
        if s[i] == '.':
            count += 1
            if i == n-1:
                si.append(count)

        else:

            si.append(count)
            count = 1
            flag = 1
            if i == n-1:
                k.append(count)
                flag2 = 1

    else:
        if s[i] == '#':
            count += 1
            if i == n-1:
                k.append(count)
                flag2 = 1
        else:
            k.append(count)
            count = 1
            flag = 0
            if i == n-1:
                si.append(count)

#
# if len(si)>0:
#     sa=si[0]
# else:
#     sa=0
#
# if flag2 == 1:
#     if len(k)>0:
#         ka=k[-1]
#     else: ka = 0
# else: ka = 0
#
# smax = sum(si) - sa
#
# kmax = sum(k) - ka
#
# ans = min(smax,kmax)
# print(ans)

lenall = len(si) + len(k)
ans = 1000
temp = 1000
for i in range(0,lenall):
    temp = sum(k[:int(i/2)]) + sum(si[int(i/2)+1:])
    if temp <= ans:
        ans = temp

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
S=input()
count=0
countB=0
flg=False

for i in range(N):
    if flg == False:
      if S[i] == '#':
          flg = True
          P = i
      else:
        continue

    if S[P] == S[i] :
        countB += 1
        continue
    else:
        count +=1

if count >= (N / 2):
    print(countB)
else:
    if count > countB:
        print(countB)
    else:
        print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=list(input())
t=-1
k=0
for i in range(n):
    if s[-1]=="#":
        s.pop(-1)
        k+=1
x=min(s.count("."),s.count("#"))
if x==0:
    print(k)
else:
    print(x)

Submission status: Wrong Answer

Solution code:
import sys

N = int(sys.stdin.readline().strip())
S = sys.stdin.readline().strip()

first = S[0]
end = S[-1]
array = []
tmp = first

for i, s in enumerate(S):
    if s != tmp:
        array.append(i-1)
        tmp = s

if N-1 not in array:
    array.append(N-1)

nn = len(array)
res = 0

array = list(reversed(array))
print(array)

if end == ".":
    for i, index in enumerate(array):
        if i % 2 != 0 or i == len(array)-1:
            continue
        if index - array[i+1]  > array[i+1] + 1:
            #print(i, "#")
            res += (array[i+1] + 1)
            break
        else:
            #print(i, "##")
            #print(array[i-1], index)
            res += (index - array[i+1])
else:
    for i, index in enumerate(array):
        if i % 2 == 0 or i == len(array)-1:
            continue
        if index - array[i+1]  > array[i+1] + 1:
            res += (array[i+1] + 1)
            break
        else:
            res += (index - array[i+1])
    
print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = [i for i in input()]
for i in range(N):
    if(S[i]=='#'):
        break
for j in range(N-1,-1,-1):
    if(S[j]=='.'):
        break
t = N -(j+1)
s = S[i:]

a = s.count('#')
b = s.count('.')
print(min(a-t,b))

Submission status: Wrong Answer

Solution code:
import sys

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

score1 = 0
score2 = 0
length = 1
check = S[0]
A = []
B = []
C = []
flag = 0

for i in range(N-1):
    if S[i] == S[i+1]:
        length += 1
    else:
        A.append(length)
        length = 1

if length != 1:
    A.append(length)

if len(A) != 0:
    if S[-1] != S[-2]:
        A.append(1)

if len(A) <= 1:
    print(0)
    sys.exit()

if S[0] == ".":
    A.pop(0)

if S[len(S)-1] == "#":
    A.pop(-1)

if len(A) == 0:
    print(0)
    sys.exit()

for i in range(len(A)):
    if i % 2 == 0:
        B.append(A[i])
    else:
        C.append(A[i])

ans = 10 ** 9
for i in range(len(A)):
    score = sum(A[0:i]) + sum(B[i:])
    if score < ans:
        ans = score

print(ans)

Submission status: Wrong Answer

Solution code:
n = input()
s = input()
white_change = 0 if s.count("#") == 0 else s[s.index("#"):].count(".")
a = s[::-1]
black_change = 0 if a.count(".") == 0 else a[a.index("."):].count("#")
print(min(white_change, black_change))