Problem:
 Score : 400 points    Problem Statement An altar enshrines N stones arranged in a row from left to right. The color of the i -th stone from the left (1 \leq i \leq N) is given to you as a character c_i ; R stands for red and W stands for white.  You can do the following two kinds of operations any number of times in any order:   Choose two stones (not necessarily adjacent) and swap them.  Choose one stone and change its color (from red to white and vice versa).   According to a fortune-teller, a white stone placed to the immediate left of a red stone will bring a disaster. At least how many operations are needed to reach a situation without such a white stone?      Constraints  2 \leq N \leq 200000  c_i is R or W .         Input Input is given from Standard Input in the following format:  N  c_{1}c_{2}...c_{N}       Output Print an integer representing the minimum number of operations needed.        Sample Input 1 4
WWRR      Sample Output 1 2  For example, the two operations below will achieve the objective.   Swap the 1 -st and 3 -rd stones from the left, resulting in RWWR .  Change the color of the 4 -th stone from the left, resulting in RWWW .        Sample Input 2 2
RR      Sample Output 2 0  It can be the case that no operation is needed.       Sample Input 3 8
WRWWRWRR      Sample Output 3 3

Solution (Python):


import math

if __name__=="__main__":
    n=int(input())
    s=input()
    s=list(s)
    
    mid=n//2
    z=0
    leftcount=0
    rightcount=0
    for i in range(0,n):
        if(s[i]=="R"):
            z+=1
    for i in range(0,z):
        if(s[i]=="R"):
            leftcount+=1
            
    print(z-leftcount)

Solution (Python):
n = int(input())
c = input()
sc = sorted(c)
nc = c.count("R")

cnt = 0
for i in range(nc):
    if c[i] != sc[i]:
        cnt += 1
print(cnt)

Solution (Python):
N = int(input())
c = list(input())

num_r1, num_r2 = 0, 0
num_w1, num_w2 = 0, 0
for i in range(int(N/2)):
  if c[i] == 'R':
    num_r1 += 1
  else:
    num_w1 += 1
for i in range(int(N/2), N):
  if c[i] == 'R':
    num_r2 += 1
  else:
    num_w2 += 1



count = 0
for i in range(num_r1+num_r2):
  if c[i] == 'W':
    count+=1
if count == 0:
  print(0)
else:
  print(count)

Solution (Python):
import numpy as np

n = int(input())
c = np.array(list(input()))

count = 0

n_white = np.count_nonzero(c=='W')
n_red = np.count_nonzero(c=='R')

if n_red:
    if n_white:
        p_white = np.where(c == 'W')[0].tolist()
        p_red = np.where(c == 'R')[0].tolist()
        num = min(len(p_white), len(p_red))
        i = 0
        while (i < num):
            if p_white[i] < p_red[-1-i]:
                count += 1
            i += 1

print(count)

Solution (Python):
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)

Solution (Python):
n = int(input())
s_w = input()
r_cnt = sw_cnt = 0

for s in s_w:
  if s == 'R':
    r_cnt += 1
    
for i, s in enumerate(s_w):
  if i>=r_cnt and s!= 'W':
    sw_cnt += 1
print(min(sw_cnt, r_cnt, n-r_cnt))

Solution (Python):
n = int(input())
c = input()

red = [i for i, x in enumerate(c) if x == 'R']
white = [i for i, x in enumerate(c) if x == 'W']

if len(white) == 0 or len(red) == 0:
    print(0)
    
else:
    red.sort(reverse=True)
    red_p = 0
    white_p = 0
    
    count = 0
    while(1):
        if white_p >= len(white):
            break
        elif red[red_p] < white[white_p]:
            break
        else:
            red_p += 1
            red.append(white[white_p])
            white_p += 1
            count += 1
            
    print(count)

Solution (Python):
N = int(input().strip())
C = [c for c in input().strip()]

if "W" not in C or "R" not in C:
    print(0)
    import sys; sys.exit(0)

cnt = 0

i = 0
j = len(C) - 1
while i < j:
    while C[i] == "R":
        i += 1
    while C[j] == "W":
        j -= 1
    if i > j:
        break
    cnt += 1
    C[i] = "R"
    C[j] = "W"
    i += 1
    j -= 1

print(cnt)

Solution (Python):
def main(N, C):
    counta_R = 0
    counta_W = 0
    for i, c in enumerate(C):
        if c == 'R':
            counta_R += 1
        else:
            counta_W += 1
    if counta_R == 0 or counta_W == 0:
        print(0)
        exit()
    ideal = 'R' * counta_R + 'W' * counta_W
    print(C.count('W', 0, counta_R))


if __name__ == '__main__':
    N = int(input())
    C = input()
    main(N, C)

Solution (Python):
def solve():
  N = int(input())
  S = input()
  red = [S.count('R')]*(N+2)
  white = [0]*(N+2)
  ans = float('inf')
  for i in range(N):
    red[i+2] = red[i+1]
    white[i+1] = white[i]
    if S[i]=='W':
      white[i+1] += 1
    if S[i]=='R':
      red[i+2] -= 1
  for i in range(N+2):
    if red[i]==white[i]:
      return red[i]
    if white[i]>red[i]:
      return red[i]
  return ans
print(solve())

Solution (Python):
N=int(input())
rw=list(input())
s=len(rw)
i=0
j=s-1
count=0

while i<j:
  while (j>=0 and rw[j]=='W'):
        j-=1
  while (i<=s-1 and rw[i]=='R'):
        i+=1
  if i>=j:
    break
  if rw[i]=='W' and rw[j]=='R':
      rw[i]='R'
      rw[j]='W'
      count+=1
  i+=1
  j-=1

j=s-1
while (rw[j]=='W' and j>=0):
      j-=1
i=0 
while (i<j):
  if (rw[i]=='W'):
      count+=1
  i+=1

print(count)

Solution (Python):
n = int(input())
C = [c for c in input()]
Red = []
White = []
len_Red = 0
len_White = 0
for i in range(n):
    if C[i] == 'R':
        Red.append(i)
        len_Red += 1
    else:
        White.append(i)
        len_White += 1

ans = 0
cnt = 0
w = 0
r = len_Red - 1
while True:
    if w < len_White:
        cnt = White[w]
        w += 1
    else:
        print(ans)
        exit()
    if r >= 0 and cnt < Red[r]:
        C[cnt], C[r] = C[r], C[cnt]
        ans += 1
        r -= 1
    else:
        print(ans)
        exit()

Solution (Python):
N = int(input())
c = 'R' + input() + 'W'

left = 0
right = N + 1
count = 0

while left <= right:
    if c[left] == 'W' and c[right] == 'R':
        count += 1
        left += 1
        right -= 1
    if c[left] == 'R':
        left += 1
    if c[right] == 'W':
        right -= 1

print(count)

Solution (Python):
N = int(input())
c = input()
var = 0
i = 0
j = 0
while True:
  if i+j >= N:
    break
  else:
    if c[i] == "W":
      if c[-1-j] == "R":
        var += 1
        i += 1
        j += 1
      else:
        j += 1
    elif c[i] == "R":
      i += 1
print(var)

Solution (Python):
n = int(input())
c = input()
r=0
w=0
p=0
count = 0
for i in range(len(c)):
    if c[i]=="R":
        r+=1
    else:
        w+=1
white = []

for i in range(len(c)):
    if c[i]=="R":
        r-=1
        
    else:
        white.append(r)
    if r==0:
        break
if len(white)==0:
    print(0)
elif white[0]==0:
    print(0)
else:
    for i in range(len(white)):
        if white[-i-1]>=len(white)-i:
            print(len(white)-i)
            break

Solution (Python):
if __name__ == "__main__":
    N = int(input())
    c = list(input())
    j = N-1
    count = 0
    for i in range(N):
        if c[i] =='W':
            while i<j:
                if c[j]  == 'R':
                    c[i] = 'R'
                    c[j] = 'W'
                    count += 1
                    break
                j -= 1
        if j < i:
            break

    if c.count('W')<count:
        print(c.count('W'))
    elif c.count('R')<count:
        print(c.count('R'))
    else:
        print(count)

Solution (Python):
n = int(input())
c = input()

red = []
o = 1
r = 0
white = []
for i in c:
    if i == "W":
        white.append(o)
    if i == 'R':
       red.append(o);
       r += 1
    o+=1

w = n - r

rr = 0
for i in red:
    if i > r:
        rr += 1

print(rr)

Solution (Python):
n = int(input())
c = input()

r_num = c.count("R")
c_left = c[:r_num]
r_num_left = c_left.count("R")
print(r_num - r_num_left)

Solution (Python):

from collections import *
from random import *
from bisect import *
import sys
input=sys.stdin.readline
def lis():
    return list(map(int,input().split()))
def ma():
    return map(int,input().split())
def inp():
    return int(input())
n=inp()
s=input().rstrip('\n')
x=s.count('R')
if(x==0 or x==n):
    print(0)
    exit(0)
ind=[]
for i in range(n):
    if(s[i]=='R'):
        ind.append(i)
r=0
for i in range(x):
    if(s[i]=='R'):
        continue
    r+=1
print(r)

Solution (Python):
import math
import collections
import bisect
from collections import deque
from copy import copy, deepcopy
 
 
def main():
    N = int(input())
    S = input()
    l=0
    r=len(S)-1
    ans=0
    while(1):
        for i in range(l,len(S)):
            if S[i]=='W':
                l=i
                break
        for j in range(r,-1,-1):
            if S[j]=='R':
                r=j
                break
        if i<j:
            ans+=1
            l=i+1
            r=j-1
        else:
            break
    print(ans)

 
if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s=input()
isi=[]
for i in s:
    isi.append(i)

R_kosuu=isi.count("R")
irekaekosuu=0
for i in range(R_kosuu):
    if isi[i]=="W":
        irekaekosuu+=1


print(irekaekosuu)

Solution (Python):
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)

Solution (Python):
n = int(input())
cc = list(input())

l = 0
r = n - 1
count = 0
while l < r:
    while l < n and cc[l] != "W":
        l += 1
    while 0 <= r and cc[r] != "R":
        r -= 1
    if 0 <= l < r < n:
        cc[l], cc[r] = cc[r], cc[l]
        count += 1
    else:
        break
print(count)

Solution (Python):
import sys

input = sys.stdin.readline

def main():
    ans = 0
    n = int(input())
    c = input().rstrip('\n')
    r = c.count('R')
    s = 0
    for i in range(n):
        if c[i]=='R':
            w = c[s:i].count('W')
            ans += min(w,r)
            s = i
            r -= w+1
            if r <= 0:
                break
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
s=list(input())


cnt=0
Ws=s.count("W")
Rs=s.count("R")

for i in range(Rs):
    if s[i]=="R" :
        cnt+=1
        
if Rs-cnt<=N-Ws:
    if Rs-cnt<=N-Rs:
        print(Rs-cnt)

elif N-Ws<=N-Rs:
    print(N-Ws)

else:
    print(N-Rs)

Solution (Python):
N = int(input())
c = input()

left = 0
right = len(c)-1

count=0
if (right+1)%2==0:
    while (left-1)!=right:
        if c[left]=='R':
            left+=1 
        elif c[right]=='W':
            right-=1
        elif c[left]=='W' and c[right]=='R':
            left+=1
            right-=1
            count+=1
else :
    while left!=right:
        if c[left]=='R':
            left+=1 
        elif c[right]=='W':
            right-=1
        elif c[left]=='W' and c[right]=='R':
            left+=1
            right-=1
            count+=1


print(count)

Solution (Python):
N=int(input())
C=[c for c in input()]
R,W=0,0
for c in C:
  if c=='R': R+=1
  else: W+=1
ans,l,r = 0,0,N-1
while l<r:
  if C[l] == 'W' and C[r] == 'R':
    l,r = l+1,r-1
    ans += 1
  elif C[l] == 'R': l+=1
  elif C[r] == 'W': r-=1
ans = min(ans,min(R,W))
print(ans)

Solution (Python):
def main():
    N = int(input())
    Cs = list(input())
    
    r = Cs.count('R')
    w = Cs.count('W')
    lis = ['R']*r +['W']*w
    
    count=0
    for i in range(len(lis)):
        if Cs[i]!=lis[i]:
            count+=1
    

    return int(count/2) if count%2==0 else int(count/2+1) 

if __name__ == '__main__':
    print(main())

Solution (Python):
n = int(input())
s = input()
ls = []
for i in range(n):
    ls.append(s[i])
l = 0
r = n - 1
su = 0
while l < r:
    while ls[l] == 'R':
        l += 1
        if l >= n:
            break
    while ls[r] == 'W':
        r -= 1
        if r < 0:
            break
    if l >= r:
        break
    else:
        su += 1
        l += 1
        r -= 1
print(su)

Solution (Python):


def main():
    N = int(input())
    c = input()
    ans = 0
    j = N - 1
    for i in range(N):
        if c[i] == 'W':
            while j > i:
                if c[j] == 'R':
                    ans += 1
                    j -= 1
                    break
                j -= 1
            else:
                break
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

import numpy as np

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


def check_condition(S):
    N = len(S)
    ind = np.where(S=="R")[0]
    if len(ind) == 0 or len(ind) == N:
        return True
    
    if np.max(ind) == len(ind) - 1:
        return True
    
    return False

def alter(S):
    if check_condition(S):
        return 0
    ind = np.where(S=="R")[0]
    num_r = len(ind)
    num_offset = np.sum(ind > num_r - 1)
    return num_offset

S = np.array(list(S))
print(alter(S))

Solution (Python):
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")
        
        
        C=C[(n-tmpw):]
            
        print(C.count("R"))

Solution (Python):
N = int(input())
S = []
c = input()
S.extend(c)

if "W" in S and "R" in S:
    t = S.count("R")
    S = S[:t]
    r = S.count("W")
    print(r)
else:
    print(0)

Solution (Python):
n = int(input())
k =  input()
stone = list(k)
w = 0
r = 0
for i in range(n):
    if stone[i] == 'R':
        r += 1
    else:
        w += 1

ans = 0
w_s = 0
r_s = 0
for i in range(n):
    if i <= r-1 and stone[i] ==  'W':
        w_s += 1
    elif i > r-1 and stone[i] == 'R':
        r_s += 1
ans = max(w_s, r_s)
print(ans)

Solution (Python):
N = int(input())
C = input()
rcount = C.count("R")

ans = 0
w = 0
r = 0
for i in range(rcount):
    if C[i] == "W":
        if r > 0:
            r -= 1
            ans += 1
        else:
            w += 1
    j = N - i - 1
    if C[j] == "R":
        if w > 0:
            w -= 1
            ans += 1
        else:
            r += 1
ans += w
print(ans)

Solution (Python):
n = int(input())
c = list(input())
ans = 0
counta=0
a = [0 for i in range(n)]
b = [0 for i in range(n)]
for i in range(n):
	if c[i]=='R':
		a[i] = 1

w = a.count(0)
r = a.count(1)
for j in range(r):
	b[j] = 1
if a==b:
	pass
else:
	for k in a:
		counta+=1
		if k==0:
			ans+=1
		if counta==r:
			break
print(ans)

Solution (Python):



import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N = i2n()
    c = list(i2s())
    l = 0
    r = N - 1
    ans = 0
    while True:
        while l < N and c[l] != 'W':
            l += 1
        while r >= 0 and c[r] != 'R':
            r -= 1
        if l < r and c[l] == 'W' and c[r] == 'R':
            c[l] = 'R'
            c[r] = 'W'
            ans += 1
        else:
            break
    print(ans)
    return

main()

Solution (Python):

N = int(input())
c = list(input())
li = 0

import sys
if "R" not in c or "W" not in c:
    print (0)
    sys.exit()
    
ans = 0
for i in range(N-1,-1,-1):

    if c[i] == "R":
        while c[li] != "W" and li <= i:
            li += 1
        if li >= i:
            break
        c[i] = "W"
        c[li] = "R"
        ans += 1
print (ans)

Solution (Python):
import itertools
import functools
import math
from collections import Counter
from itertools import combinations
import re

def main_chk():
    N=int(input())
    C=input()

    chk = N - 1
    cnt = 0

    for i in range(N):
        if C[i] == 'W':
            p = chk
            chk = i
            for x in range(N-i):
                if C[p-x] == 'R':
                    chk = p - x
                    break
            if chk <= i:
                break
            else:
                cnt = cnt + 1
                chk = chk - 1
                if chk <= i + 1:
                    break

    print(cnt)

main_chk()

Solution (Python):
n = int(input())
c = input()
l = 0
j = 0
a = [0]*n
b = [0]*n
for i in range(n):
  if(c[i]=='W'):
    a[l] =i+1
    l +=1
l = 0
for i in range(n-1,0,-1):
  if(c[i]=='R'):
    b[j] = i+1
    j +=1
j = 0
while(a[l] < b[l] and not(a[l]==0 or b[l] == 0)):
      a[l]=0
      b[l]=0
      l+=1
      j+=1
print(j)

Solution (Python):
n = int(input())
s = input()
r,w = 0,0
for i in range(n):
    if(s[i]=='W'):
        w+=1
    else:
        r+=1
ans = 0
if(w<=r):
    for i in range(n-1, n-w-1, -1):
        if(s[i]!='W'): ans+=1
else:
    for i in range(r):
        if(s[i]!='R'): ans+=1
print(ans)

Solution (Python):

n = int(input())
t = input()
*c, = map(int, t.replace(*"W1").replace(*"R0"))

"""
1100 -> 0101 -> 0011
00
10110100 -> 00111100 -> 00011110 -> 00001111
"""

a = 0
i, j = 0, 1
while i + j < n:
    if c[i] == 1 and c[-j] == 0:
        a += 1
        i += 1
        j += 1
    elif c[i] == c[-j] == 0:
        i += 1
    elif c[i] == c[-j] == 1:
        j += 1
    else:
        i += 1
        j += 1
print(a)

Solution (Python):
N = int(input())

C = input()

if N % 2 == 0:
  mid = N//2
else:
  mid = N//2+1

Rcount = C.count("R")
rcount = C.count("R", 0, Rcount)
print(Rcount-rcount)

Solution (Python):
def main():
    n = int(input())
    word = list(input())
    l, r = 0, n - 1
    cnt = 0
    while l < r:
        while l < r and word[l] == 'R':
            l += 1
        while l < r and word[r] == 'W':
            r -= 1
        if l < r:
            word[l], word[r] = word[r], word[l]
            cnt += 1
            l += 1
            r -= 1
    print(cnt)
  
if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
stones = [l for l in input()]

cnt = 0
memo = N-1
for i in range(N):
    for j in range(memo, i , -1):
        if stones[i] == 'R':
            break
        else :
            if stones[j] == 'R':
                stones[i] = 'R'
                stones[j] = 'W'
                cnt += 1
                break
    memo = j
    if memo == i:
        break
            
print(cnt)

Solution (Python):
N = int(input())
c = input()

r_c = c.count('R')
w_c = c.count('W')

c_left = c[:r_c]
print(c_left.count('W'))

Solution (Python):
N=int(input())
s=input()
l=[]
c=0
for i in range(N):
  l.append(s[i])
  if s[i]=="R":
    c+=1
a=0
for i in range(c):
  if l[i]=="W":
    a+=1
print(a)

Solution (Python):



import io
import sys
import math


_DEB = 0  

_INPUT = """\
200000
"""+("R"*200000)
_EXPECTED = """\
2

"""
if _DEB:
    sys.stdin = io.StringIO(_INPUT)
    print("!! Debug Mode !!")

def logd(str):
    if _DEB: print(f"[deb] {str}")



n = int(input())
c = input()
c=c.replace("R","0")
c=c.replace("W","1")
c=[int(x) for x in c]


def solve():
    
    ans=0
    id1=0
    for i in reversed(range(n)):
        if id1 > i:
            break
        id0=-1
        if c[i] == 0:
            id0=i
            not_found=True
            for j in range(id1,id0):
                if c[j]==1:
                    id1=j
                    c[i]=1
                    c[j]=0
                    ans+=1
                    not_found=False
                    break
            if not_found:
                break
                
            if[i]==0:
                c[i]=1
                ans+=1
        

    
    ans = str(ans)
    print(ans)
    return ans


if __name__ == "__main__":
    ans = solve()

    
    if _DEB:
        if _EXPECTED.strip() == ans.strip(): print("\n!! Success !!")
        else: print(f"\n!! Failed... !!\nANSWER:   {ans}\nExpected: {_EXPECTED}")

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate 

from collections import deque,defaultdict,Counter
import decimal
import re












import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
C = input()
flag = False
cnt = 0
for i in range(n):
    if not flag and C[i] == 'W':
        flag = True
    if flag:
        if C[i] == 'R':
            cnt += 1
        
        
        
        
        
        
        
        
r = C.count('R')
w = n-r
nya = 0
for i in range(r,n):
    if C[i] == 'R':
        nya += 1
pya = 0
for i in range(r):
    if C[i] == 'W':
        pya += 1
a = 0
for i in range(n-1):
    if C[i] == 'W' and C[i+1] == 'R':
        a += 1
print(min(max(a,cnt), nya, pya))

Solution (Python):
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)

Solution (Python):
n = int(input())
s = input()
l_s = len(s)
l = [0 for x in range(l_s)]
for i in range(l_s):
    if s[i] == 'W':
        l[i] = 1
    else:
        l[i] = 0
p_0 = l_s-1
p_1 = 0
cnt = 0
while (p_0 >= p_1+1):
    while (l[p_0] != 0 and p_0 != 0):
        p_0 -= 1
    while (l[p_1] != 1 and p_1 != l_s-1):
        p_1 += 1
    if p_0 > p_1:
        cnt += 1
    p_0 -= 1
    p_1 += 1
print(cnt)

Solution (Python):
n = int(input())
c = list(input())
c_sort = sorted(c)

count = 0

for i in range(n):
    if c[i]  != c_sort[i]:
        count += 1
print(count//2)

Solution (Python):
n=int(input())
a=input()
con=0
ans=n
for i in range(n):
  if i==ans:
      break
  if a[i]=="W":
    con+=1
    while ans>=0:
      ans-=1
      if i==ans:
        print(con-1)
        exit()
      if a[ans]=="R":
        break  
print(con)

Solution (Python):
n=int(input())
c=list(input())
w=[i for i, x in enumerate(c) if x == 'W']
r=[i for i, x in enumerate(c) if x == 'R']
wc=len(w)
rc=len(r)
wt=list(range(rc,wc+rc))
tand = set(w) & set(wt)
print(wc-len(tand))

Solution (Python):
_ = input()
c = input()
r_num = c.count("R")
print(c[:r_num].count("W"))

Solution (Python):
N = int(input())
c = input()

cR = c.count('R')
cW = c.count('W')


if all([i == 'R' for i in c[:cR]]):
    print(0)
    exit()

if all([i == 'W' for i in c[cR:cR+cW]]):
    print(0)
    exit()

ans = c[:cR].count('W')

print(ans)

Solution (Python):
numStn = int(input())
stnData = list(input())

ttlW = stnData.count("W")
ttlR = stnData.count("R")

cnt = ttlR - stnData[:ttlR].count("R")

print(cnt)

Solution (Python):
import numpy as np
n = int(input())
arr = input()

dp = [[0] * (n + 1) for _ in range(2)]
dp[1][0] = arr.count("R")

for i in range(1, n + 1):
    if arr[i - 1] == "W":
        dp[0][i] = dp[0][i - 1] + 1
        dp[1][i] = dp[1][i - 1]
    if arr[i - 1] == "R":
        dp[0][i] = dp[0][i - 1]
        dp[1][i] = dp[1][i - 1] - 1


dp = np.array(dp)

print(dp.min(axis=0).max())

Solution (Python):
import sys
sys.setrecursionlimit(10**6)

n = int(input())
C = input()

C = list(C)

d = {0:0}
tot = 0

for i,c in enumerate(C):
    if c == 'R':
        tot += 1
    d[i+1] = tot

ans = tot-d[tot]
print(ans)

Solution (Python):
N = int(input())
S = input().split()[0]

length = len(S)

W_count = S.count('W')
R_count = S.count('R')

c = 0
if R_count < W_count:
    
    for i in range(R_count):
        s = S[i]
        if s !='R':
            c+=1
        
            
else:
    for i in range(W_count):
        s = S[-1-i]
        if s != 'W':
            c+=1
print(c)

Solution (Python):
n = int(input())
s = input()
num_r = s.count("R")
slist = list(s)
sstr = str(slist[0:num_r])
count = sstr.count("W")
print (count)

Solution (Python):

N=int(input())
C=str(input())

R_list=C.split("W")
numR=len(''.join(R_list))
numW=N-numR

ans=min(numW,numR)

indR=numR

CRlis=''.join(list(C)[indR:])


R_Clis=CRlis.split("W")
numCR=len(''.join(R_Clis))

if numCR<ans:
    ans=numCR
print(ans)

Solution (Python):
n = int(input())
a = str(input())

w_list = [0 for i in range(n+1)]
r_list = [0 for i in range(n+1)]

w = 0
r = 0
f = True
for i in range(1,n+1):
    if a[i-1] == "W":
        w += 1
        if w == 1:
            f = True
    else:
        f = False
    w_list[i] = w

for i in range(n-1,-1,-1):
    if a[i] == "R":
        r += 1
    r_list[i] = r

if w==0 or r == 0 or f == True:
    print(0)
    exit()

ans = n
for i in range(n+1):
    ans = min(ans,max(w_list[i],r_list[i]))
print(ans)

Solution (Python):
n = int(input())
c = list(input())
sc = list(reversed(c))
ans = 0
r = 0
flg  = False

if 'W' not in set(c) or 'R' not in set(c):
    print(ans)


else:
    for i in range(n):
        if flg:
            break
        if c[i] == 'W':
            for j in range(r, n):
                r += 1
                if sc[j] == 'R':
                    if n >= i+j+2:
                        c[i] = 'R'
                        c[-1*j-1] = 'W'
                        ans += 1
                        break
                    else:
                        flg = True
                        break

                
            

    print(ans)

Solution (Python):
N = int(input())
C = list(input())

move_right = 0
move_left = N-1

count = 0

while move_right < move_left:
    if C[move_right] == 'R':
        move_right += 1

    else:
        while C[move_left] != 'R' and move_left >= 0:
            move_left -= 1

        if move_left <= move_right:
            
            if C[move_right + 1] == 'R':
                count += 1

        
        elif C[move_left] == 'R':
            count += 1

        move_right += 1
        move_left -= 1


print(count)

Solution (Python):
N = int(input())
C = input()

from collections import Counter

if "R" not in C:
    print(0)
    exit()
count = 0

c = Counter(C)
k = c["R"]
D = C[:k]
d = Counter(D)
print(d["W"])

Solution (Python):
N = int(input())
c = input()

x =[0] * N

ww = 0
rr = 0
for i, cc in enumerate(c):
    if cc == "R":
        x[i] = 1
        rr += 1
    else:
        ww += 1

cmd = 0
for i in range(rr):
    if x[i] == 0:
        cmd += 1

cmd2 = 0
for j in range(rr+1, len(x)):
    if x[j] == 1:
        cmd2 += 1


print(max(cmd, cmd2))

Solution (Python):
def main():
    n = int(input())
    c = [i for i, achr in enumerate(input()) if achr=="R"]
    most_right_r = len(c)
    if most_right_r==n:
        print("0")
        exit()

    count = 0
    pre_r_pos = -1
    for i, r_pos in enumerate(c):
        diff_r_pos = r_pos - pre_r_pos - 1
        if diff_r_pos and i<most_right_r:
            if diff_r_pos < most_right_r - i:
                most_right_r -= diff_r_pos
                count+=diff_r_pos
                
            else:
                count+=most_right_r - i - 1
                most_right_r -= most_right_r - i - 1
                count+=1
                
                break
        pre_r_pos = r_pos

    print(count)

if __name__=="__main__":
    main()

Solution (Python):
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)

Solution (Python):

import sys 
from collections import deque
sys.setrecursionlimit(10**6)
MOD = 10**9+7 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
S = readline().decode().rstrip()
W_cnt = S.count('W')
R_cnt = S.count('R')
ans = 0
W_ind = []
R_ind = []
for i in range(N):
    if S[i] == 'W':
        W_ind.append(i)
    else:
        R_ind.append(i)
for w in W_ind:
    if len(R_ind) == 0:
        break
    r = R_ind.pop()
    if r < w:
        break
    ans += 1
print(ans)

Solution (Python):
import sys
import numpy as np
from math import ceil as C, floor as F, sqrt
from collections import defaultdict as D
from functools import reduce as R
import heapq as H

ALP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
alp = 'abcdefghijklmnopqrstuvwxyz'
def _X(): return sys.stdin.readline().rstrip().split(' ')
def _S(ss): return tuple(ss) if len(ss) > 1 else ss[0]
def S(): return _S(_X())
def Ss(): return list(S())
def _I(ss): return tuple([int(s) for s in ss]) if isinstance(ss, tuple) else int(ss)
def I(): return _I(S())
def _Is(ss): return list(ss) if isinstance(ss, tuple) else [ss]
def Is(): return _Is(I())

n = I()
stones = S()
i, j = 0, len(stones)-1
ans = 0

if 'WR' not in stones:
  print('0')
else:
  stones = list(stones)
  while i < j:
    while stones[i] == 'R':
      i += 1
    while stones[j] == 'W':
      j -= 1
    if i < j:
      stones[i] = 'R'
      stones[j] = 'W'
      i += 1
      j -= 1
      ans += 1
  print(ans)

Solution (Python):
n = int(input())
c = input()
strings = "R"*c.count("R") + "W"*c.count("W")
ans = 0
for a,b in zip(strings,c):
    if a != b: ans += 1
print(ans//2)

Solution (Python):
input()
c=input()
a=0
while 1:
	w=c.find('W')
	r=c.rfind('R')
	if w<0 or r<0 or r<w:break
	c=c[:w]+'R'+c[w+1:r]+'W'+c[r+1:]
	a+=1
print(a)

Solution (Python):

import sys
from collections import deque

rl = sys.stdin.buffer.readline
INF = sys.maxsize

X = int(rl())
SS = [c for c in rl().strip()]

W = ord(b'W')
R = ord(b'R')

WP = []
RP = []
for i in range(len(SS)):
  if SS[i] == W:
    WP.append(i)
  elif SS[i] == R:
    RP.append(i)

RP.reverse()

WP = deque(WP)
RP = deque(RP)

sc = 0
while len(WP) != 0 and len(RP) != 0:
  w = WP.popleft()
  r = RP.popleft()
  if w < r:
    sc += 1

print(sc)

Solution (Python):
n=int(input())
li = list(input())
i = 0
k = 1
counter = 0
done = 0
while True:
    
    
    
    if done == 1:
        
        break
    if i+k == n:
        
        break
    if li[i] == "W":
        
        if k+i >= n:
            
            break
        while True:
            if li[-k] == "R":
                li[i] = "R"
                li[-k] = "W"
                counter += 1
                k += 1
                break
            k += 1
            if k+i == n:
                done = 1
                if li[-k] == "R":
                    li[-k] = "W"
                    counter += 1
                break
    i += 1
print(counter)

Solution (Python):
N = int(input())
c = input()
left = 0
right = N - 1

w_idx = -1
r_idx = -1

ans = 0

while left < right:
    if w_idx == -1 and c[left] == "W":
        w_idx = left
    if w_idx == -1 and c[left] == "R":
        left += 1

    if r_idx == -1 and c[right] == "R":
        r_idx = right
    if r_idx == -1 and c[right] == "W":
        right -= 1

    if w_idx != -1 and r_idx != -1:
        ans += 1
        w_idx = -1
        r_idx = -1
        left += 1
        right -= 1

print(ans)

Solution (Python):
N = int(input())
C = list(input())
C2 = sorted(C[:])
r_n = C.count("R")

ans = 0
for i in range(r_n):
    if C[i] != C2[i]:
        ans += 1

print(ans)

Solution (Python):
n = int(input())
st = input()

r = st.count("R")

count = 0

for i in range(r):
    if st[i]=="W":
        count += 1

print(count)

Solution (Python):


N = int(input())
S = input()
scnt = S.count("R")

print(S[:scnt].count("W"))