Buggy python submission (verdict: Time Limit Exceeded):

n=int(input())
s=str(input())
"""
n = 8
s = ".#####.#"
print(s)
print(len(s))
"""
def search(n,s):
    memo = []
    countwb=0
    now = s[0]
    count = 0
    if now == "#":
        memo.append(0)
    while(count<n):
        if s[count]==now:
            countwb+=1
        else:
            memo.append(countwb)
            countwb = 1
            now = s[count]
        count+=1
    memo.append(countwb)
    #print("memo",memo)

    count_b = []
    c_b = 0
    for  i in range(1,len(memo),2):
            c_b += memo[i]
            count_b.append(c_b)
    #print(count_b)

    count_w = []
    c_w = 0
    for  i in range(0,len(memo),2):
            c_w += memo[i]
            count_w.append(c_w)
    #print(count_w)

    #白白．．．黒黒にする
    w_b = 0
    if len(memo)%2==1:
        #最後が白
        w_b += memo[-1]
        if len(count_w)>1:
            new_n = n -  memo[-1]
            new_s = s[:int(-1*memo[-1])]
            #print("new_n",new_n)
            #print("new_s",new_s)
            w_b += search(new_n,new_s)
    else:
        #最後が黒
        if len(count_b)>1:
            new_n = n -  memo[-1]
            new_s = s[:int(-1*memo[-1])]
            #print("new_n",new_n)
            #print("new_s",new_s)
            w_b += search(new_n,new_s)
    #print("w_b",w_b)
    #全部白にする
    allw = 0
    if count_b!=[]:
        allw = count_b[-1]
    #print("allw",allw)
    #全部黒にする
    allb = 0
    if count_w!=[]:
        allb = count_w[-1]
    #print("allb",allb)

    return min(w_b,allb,allw)

print(search(n,s))

Fixed submission:
n=int(input())
s=str(input())

#n = 9
#s = "........."
#print(s)
#print(len(s))

memo = []
countwb=0
now = s[0]
count = 0
if now == "#":
    memo.append(0)
while(count<n):
    if s[count]==now:
        countwb+=1
    else:
        memo.append(countwb)
        countwb = 1
        now = s[count]
    count+=1
memo.append(countwb)
#print(memo)

memo_r=[]
for i in range(len(memo)):
    memo_r.append(memo[-1*(i+1)])
#print(memo_r)

count_w = []
c_w = 0
if len(memo_r)%2==1:
    count_w.append(0)
    for  i in range(0,len(memo_r),2):
            c_w += memo_r[i]
            count_w.append(c_w)
else:
    count_w.append(0)
    count_w.append(0)
    for  i in range(1,len(memo_r),2):
            c_w += memo_r[i]
            count_w.append(c_w)
#print(count_w)

count_b = []
count_b.append(0)
count_b.append(0)
c_b = 0
for  i in range(1,len(memo),2):
        c_b += memo[i]
        count_b.append(c_b)
#print(count_b)

count_list = []
for i in range(len(count_b)):
    temp = count_w[-1*(i+1)]+count_b[i]
    count_list.append(temp)
#print(count_list)

print(min(count_list))

Buggy python submission (verdict: Runtime Error):
n=int(input())
s=str(input())
"""n = 8
s = "#.....#."
print(s)
print(len(s))"""

memo = []
countwb=0
now = s[0]
count = 0
if now == "#":
    memo.append(0)
while(count<n):
    if s[count]==now:
        countwb+=1
    else:
        memo.append(countwb)
        countwb = 1
        now = s[count]
    count+=1
memo.append(countwb)
#print(memo)
#print(memo[len(memo)-3])


count_b = []
c_b = 0
for  i in range(1,len(memo),2):
        c_b += memo[i]
        count_b.append(c_b)
#print(count_b)

count_w = []
c_w = 0
for  i in range(0,len(memo),2):
        c_w += memo[i]
        count_w.append(c_w)
#print(count_w)

#白白．．．黒黒にする
w_b = 0
if len(memo)%2==1:
    #最後が白
    w_b += memo[-1]
    if len(count_b)>1:
        w_b += count_b[-2]
else:
    #最後が黒
    if len(count_b)>1:
        w_b += count_b[-2]
#print("w_b",w_b)
#全部白にする
allw = count_b[-1]
#print("allw",allw)
#全部黒にする
allb = count_w[-1]
#print("allb",allb)

print(min(w_b,allb,allw))

Fixed submission:
n=int(input())
s=str(input())

#n = 9
#s = "........."
#print(s)
#print(len(s))

memo = []
countwb=0
now = s[0]
count = 0
if now == "#":
    memo.append(0)
while(count<n):
    if s[count]==now:
        countwb+=1
    else:
        memo.append(countwb)
        countwb = 1
        now = s[count]
    count+=1
memo.append(countwb)
#print(memo)

memo_r=[]
for i in range(len(memo)):
    memo_r.append(memo[-1*(i+1)])
#print(memo_r)

count_w = []
c_w = 0
if len(memo_r)%2==1:
    count_w.append(0)
    for  i in range(0,len(memo_r),2):
            c_w += memo_r[i]
            count_w.append(c_w)
else:
    count_w.append(0)
    count_w.append(0)
    for  i in range(1,len(memo_r),2):
            c_w += memo_r[i]
            count_w.append(c_w)
#print(count_w)

count_b = []
count_b.append(0)
count_b.append(0)
c_b = 0
for  i in range(1,len(memo),2):
        c_b += memo[i]
        count_b.append(c_b)
#print(count_b)

count_list = []
for i in range(len(count_b)):
    temp = count_w[-1*(i+1)]+count_b[i]
    count_list.append(temp)
#print(count_list)

print(min(count_list))