qyliang/CodeNanoFix huggingface dataset:
Buggy code:
n=int(input())
s=input()
l,r=0,n-1
ans=0
while l<r:
  cnt=0
  while r>=0 and s[r]=='W':r-=1
  while l<n and s[l]=='R':l+=1
  if l>=r:break
  while s[r]=='R':
    cnt+=1
    r-=1
  cm=cnt
  while cnt:
    if s[l]=='W':cnt-=1
    l+=1
    if l==r:break
  ans+=abs(cm-cnt)
print(ans)

Fixed code:
n=int(input())
s=input()
l,r=0,n-1
ans=0
while l<r:
  cnt=0
  while r>=0 and s[r]=='W':r-=1
  while l<n and s[l]=='R':l+=1
  if l>=r:break
  while s[r]=='R':
    cnt+=1
    r-=1
  cm=cnt
  while cnt:
    if s[l]=='W':cnt-=1
    l+=1
    if l>r:break
  ans+=cm-cnt
print(ans)
Buggy code:
N = int(input())
S = input()
A = int(S.count("R"))
if A == N:
  print(A)
  exit()
s = S[:A]
B = int(s.count("R"))
print(A-B)

Fixed code:
N = int(input())
S = input()
A = int(S.count("R"))
if A == N:
  print(0)
  exit()
s = S[:A]
B = int(s.count("R"))
print(A-B)
Buggy code:
N=int(input())
C=input()
from collections import Counter
count=Counter(C)
#print(count)
countR=count["R"]
countW=count["W"]
missR=0
missW=0
for i in range(N):
    now=C[i]
    if now=="W" and i>=N-countW:
        missW+=1
    if now=="R" and i<countR:
        missR+=1
# print(countR, countW)
X=min(missW, missR)
if X!=0:
    ans=min(missW, missR)+abs(missR-missW)
else:
    ans=0
print(ans)

Fixed code:
N=int(input())
C=input()
from collections import Counter
count=Counter(C)
#print(count)
countR=count["R"]
countW=count["W"]
missR=0
missW=0
for i in range(N):
    now=C[i]
    if now=="W" and i<N-countW:
        missW+=1
    if now=="R" and i>countR:
        missR+=1
# print(countR, countW)
X=min(missW, missR)

ans=min(missW, missR)+abs(missR-missW)

print(ans)
#print(missR, missW)
Buggy code:
s = input()
R_num = s.count("R")
s[:R_num].count("W")


Fixed code:
s = input()
s = input()

R_num = s.count("R")
print(s[:R_num].count("W"))
Buggy code:
N = int(input())
S = input()

prev = S[0]
cur = 'R'
count = 0
wNum = 0
changedPoint = -1
changed = False
for i in range(N-1, -1, -1):
    cur = S[i]
    changed = False
    if cur == 'R':
        for j in range(wNum, i):
            wNum = j + 1
            if S[j] == 'W':
                changedPoint = j
                changed = True
                break
        if changed == True:
            count += 1
        elif changedPoint != -1 and wNum != i:
            count += 1
            break
        else:
            break

    prev = cur
    if wNum == i-1:
        break

print(count)


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

prev = S[0]
cur = 'R'
count = 0
wNum = 0
changedPoint = -1
changed = False
for i in range(N-1, -1, -1):
    cur = S[i]
    changed = False
    if cur == 'R':
        for j in range(wNum, i):
            wNum = j + 1
            if S[j] == 'W':
                changedPoint = j
                changed = True
                break
        if changed == True:
            count += 1
        elif changedPoint != -1 and wNum < i:
            count += 1
            break
        else:
            break

    prev = cur
    if wNum == i-1:
        break

print(count)
Buggy code:
N = int(input())
C = []
for c in list(input()):
  a = 0
  if c == "R":
    a = 1
  C.append(a)
L = -1
R = N
cnt = 0
while L < R:
  L += 1
  if C[L] != 0:
    continue
  while L < R:
    R -= 1
    if C[R] != 1:
      continue
    cnt += 1
    break     
print(cnt)
  

Fixed code:
N = int(input())
C = []
for c in list(input()):
  a = 0
  if c == "R":
    a = 1
  C.append(a)
L = -1
R = N
cnt = 0
while L < R:
#  print(str(L) + " " + str(R))
  L += 1
  if L >= R:
    break
  if C[L] != 0:
    continue
  while L < R:
    R -= 1
    if L >= R:
      break
    if C[R] != 1:
      continue
    cnt += 1
    break     
print(cnt)
Buggy code:
N = int(input())
c = input()
W = 0
R = 0

for x in c:
  if x=='R':
    R += 1
    
ans = R
for x in c:
  if c=='W':
    W += 1
  else:
    R -= 1
    
  ans = min(ans,max(R,W))
  
print(ans)

Fixed code:
N = int(input())
c = input()
W = 0
R = 0

for x in c:
  if x=='R':
    R += 1

ans = R
for x in c:
  if x=='W':
    W += 1
  else:
    R -= 1
    
  ans = min(ans,max(R,W))
  
print(ans)
Buggy code:
N = int(input())
c = input()
red_to_white = c.count("R")
ans = red_to_white
while_to_red = 0

for i in c:
    if i == "W":
        while_to_red += 1 
    else:
        red_to_white -= 1
    ans = min(ans,max(while_to_red,red_to_white))
    print(while_to_red)
    print(red_to_white)

print(ans)

Fixed code:
N = int(input())
c = input()
red_to_white = c.count("R")
ans = red_to_white
while_to_red = 0

for i in c:
    if i == "W":
        while_to_red += 1 
    else:
        red_to_white -= 1
    ans = min(ans,max(while_to_red,red_to_white))

print(ans)
Buggy code:
n=int(input())
C=list(input())
if C.count("W")==0:
    print(0)
elif C.count("W")==1:
    if C[-1]=="W":
        print(0)
    else:
        print(1)
else:
    if n%2==0:
        tmpw=C.count("W")
        print(tmpw,C)
        C=C[tmpw:]
        print(tmpw,C)
        print(C.count("R"))
    else:
        tmpw=C.count("W")
        print(tmpw,C)
        C=C[tmpw-1:]
        print(tmpw,C)
        print(C.count("R"))
    
        

Fixed code:
n=int(input())
C=list(input())
if C.count("W")==0:
    print(0)
elif C.count("W")==1:
    if C[-1]=="W":
        print(0)
    else:
        print(1)
else:
        tmpw=C.count("W")
        #print(tmpw,C)
        #if tmpw%2==0:
        C=C[(n-tmpw):]
            #print(tmpw,C)
        print(C.count("R"))
        #else:
            #C=C[tmpw-1:]
            #print(tmpw,C)
            #print(C.count("R"))
Buggy code:
n = int(input())
c = input()

t = n
lw, rr = 0, c.count('R')
for ci in c:
    swap = min(lw, rr)
    wtor, rtow = max(0, lw-swap), max(0, rr-swap)
    t = min(t, swap + wtor + rtow)
    lw = lw + (ci == 'W')
    rr = rr - (ci == 'R')

print(t)


Fixed code:
n = int(input())
c = input()

t = n
lw, rr = 0, c.count('R')
for ci in c+'Z':
    swap = min(lw, rr)
    wtor, rtow = max(0, lw-swap), max(0, rr-swap)
    t = min(t, swap + wtor + rtow)
    lw = lw + (ci == 'W')
    rr = rr - (ci == 'R')

print(t)
Buggy code:
n=int(input())
c=list(input())
l,r=0,n-1
ans=0
while l<r:
    while c[l]!='W':
        l+=1
        if l>n-1:
            break
    while c[r]!='R' or r>=0:
        r-=1
        if r<0:
            break
    if l<r:
        ans+=1
        l+=1
        r-=1
print(ans)

Fixed code:
n=int(input())
c=list(input())
l,r=0,n-1
ans=0
while l<r:
    while c[l]!='W':
        l+=1
        if l>n-1:
            break
    while c[r]!='R':
        r-=1
        if r<0:
            break
    if l<r:
        ans+=1
        l+=1
        r-=1
print(ans)
Buggy code:
n = int(input())
c = input()

w = []
r = []
for i in range(n):
  if c[i] == 'W':
    w.append(i+1)
  else:
    r.append(i+1)

w_len = len(w)
r_len = len(r)
if w_len == 0 or r_len == 0:
  print(0)
  exit(0)

w_min = w[0]
r_max = r[r_len-1]
count = 0
for i in range(n):
  tmp = w[i]
  w[i] = r[r_len-i-1]
  r[r_len-i-1] = tmp
  count += 1
  
  w_min = w[count]
  r_max = r[r_len - count - 1]
  
  if r_max < w_min:
    print(count)
    exit(0)
print(count)

Fixed code:
n = int(input())
c = input()

w = []
r = []
for i in range(n):
  if c[i] == 'W':
    w.append(i+1)
  else:
    r.append(i+1)

w_len = len(w)
r_len = len(r)
if w_len == 0 or r_len == 0:
  print(0)
  exit(0)

if w_len < r_len:
  search = w_len
else:
  search = r_len

w_min = w[0]
r_max = r[r_len-1]

if r_max < w_min:
  print(0)
  exit(0)

count = 0
for i in range(search):
  tmp = w[i]
  w[i] = r[r_len-i-1]
  r[r_len-i-1] = tmp
  count += 1
  
  if w_len > count and (r_len-count-1) >= 0:
    w_min = w[count]
    r_max = r[r_len - count - 1]
  else:
    w_min = w[w_len-1]
    r_max = r[0]
    
  if r_max < w_min:
    print(count)
    exit(0)
print(count)
Buggy code:
n = int(input())
s = list(input())
l = 0
r = n-1
c = 0
while l < r:
  for i in range(l, r):
    if s[i] == "W":
      l = i
      break
  else:
    l = r
  for i in range(r, l, -1):
    if s[i] == "R":
      r = i
      break
  else:
    r = l
  if l < r:
    c += 1
print(c)
      
    

Fixed code:
n = int(input())
s = list(input())
l = 0
r = n-1
c = 0
while l < r:
  for i in range(l, r):
    if s[i] == "W":
      c += 1
      l = i+1
      break
  else:
    l = r
  for i in range(r, l-1, -1):
    if s[i] == "R":
      r = i-1
      c += 1
      break
  else:
    r = l

print(c//2)
Buggy code:
n=int(input())
s=list(input())
x=[]
y=[]

for i in range(n):
  if s[i]=='R':
    x+=[i]
  elif s[i]=='W':
    y+=[i]
    
ans=0
if y==[]:
  print(ans)
else:
  while True:
    if x[-1]>y[0]:
      x.pop()
      y.pop(0)
      ans+=1
      continue
    
    else:
      print(ans)
      break

Fixed code:
n=int(input())
s=list(input())
x=[]
y=[]

for i in range(n):
  if s[i]=='R':
    x+=[i]
  elif s[i]=='W':
    y+=[i]
    
ans=0


while True:
    if len(y)==0:
      print(ans)
      break
    elif len(x)==0:
      print(ans)
      break
    elif x[-1]>y[0]:
      x.pop()
      y.pop(0)
      ans+=1
      continue
    
    else:
      print(ans)
      break
Buggy code:
n = int(input())
c = input()
count = 0

i, j = 0, n - 1
if c.count('W') == 0 | c.count('R') == 0:
    count = 0
else:
    white_cnt = []
    red_cnt = []
    for i in range(n):
        if c[i] == 'W':
            white_cnt.append(i)
        else:
            red_cnt.append(i)
while 1:
    white_num = white_cnt.pop(0)
    red_num = red_cnt.pop()
    if white_num < red_num:
        count += 1
    if len(white_cnt) == 0:
        break
    if len(red_cnt) == 0:
        break
print(count)


Fixed code:
n = int(input())
c = input()
count = 0

i, j = 0, n - 1
if c.count('W') == 0:
    count=0
elif c.count('R') == 0:
    count = 0
else:
    white_cnt = []
    red_cnt = []
    for i in range(n):
        if c[i] == 'W':
            white_cnt.append(i)
        else:
            red_cnt.append(i)
    while 1:
        white_num = white_cnt.pop(0)
        red_num = red_cnt.pop()
        if white_num < red_num:
            count += 1
        if len(white_cnt) == 0:
            break
        if len(red_cnt) == 0:
            break
print(count)
Buggy code:
N=int(input())
c=list(input())

left=0
right=N
ans=0
while left<right:
    
    print(left,right, list(range(left,N)), list(range(right-1,-1,-1)))
    for il in range(left,N):
        if c[il]=="W":
            break
    else:
        il=N
        
    for ir in range(right-1,-1,-1):
        if c[ir]=="R":
            break
    else:
        ir=-1
    
    
    if il<ir:
        c[il]="R"
        c[ir]="W"
        left=il+1
        right=ir
        ans+=1
    else:
        break
        
print(ans)

Fixed code:
N=int(input())
c=list(input())

left=0
right=N
ans=0
while True:
    
    for il in range(left,N):
        if c[il]=="W":
            break
    else:
        il=N
        
    for ir in range(right-1,-1,-1):
        if c[ir]=="R":
            break
    else:
        ir=-1
    
    
    if il<ir:
        c[il]="R"
        c[ir]="W"
        left=il+1
        right=ir
        ans+=1
    else:
        break
        
print(ans)
Buggy code:
N = int(input())
list=[]
list = (str(input()))
cnt = 0
R = 0
W = 0
ans = 0
for i in range (N):
    if list[i] == "R":
        R += 1

W = N-R

cnt = min(R, W)

if R >= W:
    for i in range(cnt):
        if list[N-cnt]=="R":
            ans +=1

if R<W:
    for j in range(cnt+1):
        if list[j] == "W":
            ans +=1


print(ans)




Fixed code:
N = int(input())
list=[]
list = (str(input()))
cnt = 0
R = 0
W = 0
ans = 0
for i in range (N):
    if list[i] == "R":
        R += 1

W = N-R

cnt = min(R, W)

if R > W:
    for i in range(0,cnt):
        if list[N-i-1]=="R":
            ans +=1

if R<=W:
    for j in range(0,cnt):
        if list[j] == "W":
            ans +=1


print(ans)
Buggy code:
from collections import deque

N = int,input()
c = input()

rd = deque([i for i, x in enumerate(c) if x == "R"])
wd = deque([i for i, x in enumerate(c) if x == "W"])
if(len(rd) == 0 or len(wd) == 0):
  print(0)
  exit()
count = 0
for i in range(len(c)):
  if(not(wd[0] < rd[-1])):
    break
  rd.pop()
  wd.popleft()
  count += 1
print(count)

Fixed code:
from collections import deque

N = int(input())
c = input()

rd = deque([i for i, x in enumerate(c) if x == "R"])
wd = deque([i for i, x in enumerate(c) if x == "W"])
if(len(rd) == 0 or len(wd) == 0):
  print(0)
  exit()
count = 0
for i in range(len(c)):
  if(not(wd[0] < rd[-1])):
    break
  rd.pop()
  wd.popleft()
  count += 1
  if(len(rd) == 0 or len(wd) == 0):
    break
print(count)
Buggy code:
N = int(input())
C=list(input())
L=0
R=0
RR=0
X=0
    for i in range(N):
      L=i
      R=RR
      if L>=N-1-R:
        break
      if C[i]=='W':
          for ii in range(N-L-R):
            if C[N-1-R-ii]=='R':
              C[L]='R'
              C[N-1-R-ii]='W'
              X+=1
              break
            RR+=1
            if L>=N-1-R:
              break
print(X)

Fixed code:
N = int(input())
C=list(input())
type=set(C)
L=0
R=0
RR=0
X=0
if 'R' in type and 'W' in type:
    for i in range(N):
      L=i
      R=RR
      if L>=N-1-R:
        break
      if C[i]=='W':
          for ii in range(N-L-R):
            if C[N-1-R-ii]=='R':
              C[L]='R'
              C[N-1-R-ii]='W'
              X+=1
              break
            RR+=1
            if L>=N-1-R:
              break
print(X)
Buggy code:
import sys
N = int(input())
c = [str(x) for x in list(input())]
R = sum([1 for i, x in enumerate(c) if(x=='R')])
W = 0
if(R==N)or(R==0):
  print(0)
  sys.exit()
ans = R
i = 0
while True:
  if(c[i] == 'W'):
    W += 1
  else:
    R -= 1
  n_ans = max(W,R)
  if(n_ans>ans):
    print(ans)
    sys.exit()
  ans = n_ans
  i += 1


Fixed code:
import sys
N = int(input())
c = [str(x) for x in list(input())]
R = sum([1 for i, x in enumerate(c) if(x=='R')])
W = 0
if(R==N)or(R==0):
  print(0)
  sys.exit()
ans = R
i = 0
while True:
  if(i==N):
    print(ans)
    sys.exit()
  if(c[i] == 'W'):
    W += 1
  else:
    R -= 1
  n_ans = max(W,R)
  if(n_ans>ans):
    print(ans)
    sys.exit()
  ans = n_ans
  i += 1
Buggy code:
s = int(input()) 
n = input()
str_list = list(n)
w_cnt = str_list.count('W')
r_cnt = str_list.count('R')
w_result = str_list[w_cnt:]
r_result = str_list[:r_cnt]
r = w_result.count('R')
w = r_result.count('W')
if w <= r:
    print(r)
else:
    print(w)


Fixed code:
s = int(input())
n = input()
str_list = list(n)
w_cnt = str_list.count('W')
r_cnt = str_list.count('R')
if w_cnt == 0 or r_cnt == 0:
    print(0)
else:
    w_result = str_list[r_cnt:]
    r_result = str_list[:r_cnt]
    r = w_result.count('R')
    w = r_result.count('W')
    if w <= r:
        print(w)
    else:
        print(r)
Buggy code:
widx = []
ridx = []
s = input()
for i, char in enumerate(s):
  if char == "W":
    widx.append(i)
  else:
    ridx.append(i)
count = 0
i = 0
j = len(ridx) - 1
while i < len(widx) and j >= 0 and widx[i] < ridx[j]:
  i += 1
  j -= 1
  count += 1
print(count)
  

Fixed code:
widx = []
ridx = []
n = int(input())
s = input()
for i, char in enumerate(s):
  if char == "W":
    widx.append(i)
  else:
    ridx.append(i)
count = 0
i = 0
j = len(ridx) - 1
while i < len(widx) and j >= 0 and widx[i] < ridx[j]:
  i += 1
  j -= 1
  count += 1
print(count)
Buggy code:
ma = lambda :map(int,input().split())
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq
n = ni()
cs = input()
r = cs.count("R")
print(cs[:r].count("R"))


Fixed code:
ma = lambda :map(int,input().split())
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq
n = ni()
cs = input()
r = cs.count("R")
print(cs[r:].count("R"))
Buggy code:
n=int(input())
a=input()
print(a)
a=list(a)
print(a)
t=0
s=0
for i in range(0,n):
    if a[i]=="R":
        t=t+1

for i in range(0,t):
    if a[i]!="R":
        s=s+1
print(s)

Fixed code:
n=int(input())
a=input()

a=list(a)

t=0
s=0
for i in range(0,n):
    if a[i]=="R":
        t=t+1

for i in range(0,t):
    if a[i]!="R":
        s=s+1
print(s)
Buggy code:
n = int(input())
s = list(input())
num = 0
k = -1
for i in range(1,n):
  if i + k == n:
    break
  if s[-i] == 'R':
    j = k+1
    while True:
      if s[j] == 'W':
        s[j] = 'R'
        s[-i] = 'W'
        k = j
        num += 1
        break
      j += 1
    
print(num)      
      
    

Fixed code:
n = int(input())
s = list(input())
num = 0
k = -1
for i in range(1,n):
  if i + k >= n-1:
    break
  if s[-i] == 'R':
    j = k+1
    while True:
      if j +i == n:
        k = j
        break
      if s[j] == 'W':
        s[j] = 'R'
        s[-i] = 'W'
        k = j
        num += 1
        break
      j += 1
      
print(num)
Buggy code:
n = int(input())
c = list(input())

counter = 0
k = 0

i = 0
j = n-1

while i <= j:
	if c[i] == 'W' and c[j] == 'R':
		counter += 1
		i += 1
		j -= 1
	else:
		j -= 1
print(counter)

Fixed code:
n = int(input())
c = list(input())

counter = 0
k = 0

i = 0
j = n-1

while i < j and i < n and 0 <= j:
	if c[i] == 'W' and c[j] == 'R':
		counter += 1
		i += 1
		j -= 1
	elif c[i] == 'W' and c[j] == 'W':
		j -= 1
	elif c[i] == 'R' and c[j] == 'R':
		i += 1
	else:
		i += 1
		j -= 1 
print(counter)
Buggy code:
n = int(input())
s = input()
red = []
white = []
for i in range(n):
  if s[i] == 'W':
    white.append(i)
for i in range(n - 1, -1, -1):
  if s[i] == 'R':
    red.append(i)
print(white, red)
if min(len(red), len(white)) == 0:
  print(0)
  exit()

for i in range(min(len(red), len(white))):
  if white[i] >= red[i]:
    print(i)
    exit()

Fixed code:
n = int(input())
s = input()
red = []
white = []
for i in range(n):
  if s[i] == 'W':
    white.append(i)
for i in range(n - 1, -1, -1):
  if s[i] == 'R':
    red.append(i)
#print(white, red)
if min(len(red), len(white)) == 0:
  print(0)
  exit()

for i in range(min(len(red), len(white))):
  if white[i] >= red[i]:
    print(i)
    exit()
print(min(len(red), len(white)))
Buggy code:
n=int(input())
c=input()
r=c.count('R')
w=c.count('W')
rch=c[:r].count('W')
wch=c[r:].count('R')
print(min(rch,wch))
print(r,w,rch,wch)

Fixed code:
n=int(input())
c=input()
r=c.count('R')
w=c.count('W')
rch=c[:r].count('W')
wch=c[r:].count('R')
print(min(rch,wch))
Buggy code:
 #https://atcoder.jp/contests/agc008/submissions/15248942
 import sys
 import math
 
 sys.setrecursionlimit(10 ** 8)
 ini = lambda: int(sys.stdin.readline())
 inm = lambda: map(int, sys.stdin.readline().split())
 inl = lambda: list(inm())
 ins = lambda: sys.stdin.readline().rstrip()
 debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))
 
 N = ini()
 c = input()
 
 R = c.count('R')
 left = c[:R]
 ans = left.count('W')
 
 print(ans)


Fixed code:
#https://atcoder.jp/contests/agc008/submissions/15248942
import sys
import math

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

N = ini()
c = input()

R = c.count('R')
W = c.count('W')
left = c[:R]

ans = min(R,W)
ans = min(ans,left.count('W'))

print(ans)
Buggy code:
num = int(input())
wr = str(input())

i_counts = []
r_num = wr.count("R")

for i in range(num):
  rw_count = r_num
  wr_count = 0
  if wr[i] == "W":
    wr_count += 1
  else:
    rw_count -= 1

  i_count = max(wr_count, rw_count)
  i_counts.append(i_count)

  ans = min(i_counts)

print(ans)

Fixed code:
num = int(input())
wr = str(input())

r_num = wr.count("R")
i_counts = [r_num]
rw_count = r_num
wr_count = 0
for i in range(num):
  if wr[i] == "W":
    wr_count += 1
  else:
    rw_count -= 1
  i_count = max(wr_count, rw_count)
  i_counts.append(i_count)

ans = min(i_counts)

print(ans)
Buggy code:
n=int(input())
a=list(input())
c=a.count("W")
b=a[:n-c]
d=b.count("W")
print(d)
print(b)

Fixed code:
n=int(input())
a=list(input())
c=a.count("W")
b=a[:n-c]
d=b.count("W")
print(d)
Buggy code:
N = int(input())
c = input()
W = 0
R = c.count('R')
if R == 0 or W == 0:
  print(0)
  exit()

ans = N
t = 0
for i in range(N):
  if c[i] == 'W':
    W += 1
  else:
    R -= 1
  t = max(R,W)
  ans = min(ans, t)
print(ans)

Fixed code:
N = int(input())
c = input()
W = 0
R = c.count('R')
if R == 0 or R == N:
  print(0)
  exit()
ans = N
t = 0
for i in range(N):
  if c[i] == 'W':
    W += 1
  else:
    R -= 1
  t = max(R,W)
  ans = min(ans, t)
print(ans)
Buggy code:
n = int(input())
c = input()

r = c.count("R")
cnt = 0
for i, j in enumerate(c):
    if i + 1 == r:
        break
    if j == "W":
        cnt += 1
print(cnt)


Fixed code:
n = int(input())
c = input()

r = c.count("R")
cnt = 0
for i, j in enumerate(c):
    if i == r:
        break
    if j == "W":
        cnt += 1
print(cnt)
Buggy code:
a=int(input())
b=input()
r_num=0
w_num=0
for i in b :

  if i=='W' :
    w_num+=1
  else :
    r_num+=1

count=0
if r_num<w_num
  for i in range(r_num) :
    if b[i]=='R' :
      count+=1
else :
  for i in range(w_num) :
    if b[i]=='W' :
      count+=1
print(count)


Fixed code:
a=int(input())
b=input()
r_num=0
w_num=0
for i in b :

  if i=='W' :
    w_num+=1
  else :
    r_num+=1

count=0

for i in range(r_num) :
  if b[i]=='W' :
    count+=1


print(count)
Buggy code:
n=int(input())
c=input()
a=[]
for i in range(n):
    if(c[i]=='R'):
        a.append(0)
    else:
        a.append(1)

zero=0
one=0
for i in range(n):
    if(a[i]==0):
        zero+=1

ans=[zero]
for i in range(n):
    if(a[i]==0):
        zero-=1
    else:
        one+=1
    #print(zero,one)
    #ans.append(max(zero,one))

print(min(ans))

Fixed code:
n=int(input())
c=input()
a=[]
for i in range(n):
    if(c[i]=='R'):
        a.append(0)
    else:
        a.append(1)

zero=0
one=0
for i in range(n):
    if(a[i]==0):
        zero+=1

ans=[zero]
for i in range(n):
    if(a[i]==0):
        zero-=1
    else:
        one+=1
    #print(zero,one)
    ans.append(max(zero,one))

print(min(ans))
Buggy code:
N = int(input())
#stones = list(input())
stones = input()
print(stones)
a = stones.count('W')
b = stones.count('R')

left_side =stones.count('W', 0, a)
print(left_side) 

Fixed code:
N = int(input())
#stones = list(input())
stones = input()
#print(stones)
a = stones.count('W')
b = stones.count('R')
 
left_side =stones.count('W', 0, b)
 
print(left_side)
Buggy code:
N = int(input())
c = input()
C = []
for c_i in c:
    C.append(c_i)
count = 0
left = N
for i in range(N):
    if i >= left:
        print(count)
        break
    if C[i] == 'W':
        for j in reversed(range(left)):
            if i == 1:
                if j == i:
                    C[i] = 'R'
                    left = i
                    break
            else:
                if j == i-1:
                    left = i
                    break
            if C[j] == 'R':
                C[i] = 'R'
                C[j] = 'W'
                left = j
                count += 1
                break

Fixed code:
N = int(input())
c = input()
C = []
for c_i in c:
    C.append(c_i)
count = 0
left = N-1
for i in range(N):
    if i >= left:
        print(count)
        break
    if C[i] == 'W':
        for j in reversed(range(left+1)):
            if i == 1:
                if j == i:
                    C[i] = 'R'
                    left = i
                    break
            else:
                if j == i-1:
                    left = i
                    break
            if C[j] == 'R':
                C[i] = 'R'
                C[j] = 'W'
                left = j
                count += 1
                break