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 sys
input = sys.stdin.readline


def read():
    N = int(input().strip())
    C = list(input().strip())
    return N, C


def solve(N, C):
    ans = 0
    l = 0
    r = N-1
    while l < r:
        while l < N and C[l] == "R":
            l += 1
        while 0 <= r and C[r] == "W":
            r -= 1
        if l < r:
            ans += 1
            l += 1
            r -= 1
    return ans


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Solution (Python):
N = int(input())
c = list(input())
r = c.count("R")
w = c.count("W")
true = []
true += ["R"]*r
true += ["W"]*w
count = 0
for num in range(N):
  if true[num] != c[num]:
    count += 1
print(int(count/2))

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

r_count = C.count("R")
ans = r_count - C[:r_count].count("R")

print(ans)

Solution (Python):
import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque

def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

'''D'''
n = r()
a = input()
whites = a.count('W')
if whites == 0 :
    print(0)
else:
    reds = n-whites
    s = 'R'*reds+'W'*whites
    change = 0
    for i in range(n):
        if a[i] != s[i]:
            change+=1
    print((change+1)//2)

Solution (Python):
n = int(input())
c = list(input())
ans = 0
cntW, cntR = 0, 0
flg = True
for i in range(n-1, -1, -1):
    if c[i] == 'W':
        cntW += 1
    else:
        cntR += 1
    if flg:
        if c[i] == 'R':
            flg = False
    else:
        if c[i] == 'W':
            ans += 1
ans = min(ans, cntW, cntR)
cnt = 0
for i in range(n-1, -1+cntR, -1):
    if c[i] == 'R':
        cnt += 1
ans = min(ans, cnt)
print(ans)

Solution (Python):
def main():
    n = int(input())
    c = input()
    i, j = 0, n-1
    ans = 0
    while i < j:
        if c[i] == "W" and c[j] == "R":
            ans += 1
            i += 1
            j -= 1
        if c[i] == "R":
            i += 1
        if c[j] == "W":
            j -= 1
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import math
import sys
import bisect


t=1
for _ in range(t):
    n=int(input())
    
    
    s=input()
    f=0
    dw=[]
    dr=[]
    for i in range(n):
        if s[i]=='W':
            dw.append(i)
        else:
            dr.append(i)
    ind=0 
    cnt=0
    
    for i in range(len(dr)-1,-1,-1):
        if ind>=len(dw):
            break
        
        if dr[i]>dw[ind]:
            cnt+=1
            ind+=1
    print(cnt)

Solution (Python):
import sys
from collections import defaultdict, deque, Counter
import math


from bisect import bisect_left, bisect_right





input = sys.stdin.readline

getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = 10 ** 20
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)


def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD


def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD


def kaijyo(n):
    ret = [1]
    for i in range(1, n + 1):
        ret.append((ret[-1] * i)% MOD)
    return ret


def solve():
    n = getN()
    S = getS()
    l = 0
    r = n - 1
    
    ansl, ansr = 0,0
    while S[r] == "W":
        r -= 1
        if l == r:
            print(max(ansl, ansr))
            return
    while True:
        while True:
            if S[l] == "W":
                l += 1
                ansl += 1
                
                while S[l] == "R":
                    if l == r:
                        print(max(ansl, ansr))
                        return
                    l += 1
                break
            l += 1
            if l == r:
                break
            
        if l == r:
            print(max(ansl, ansr))
            return

        while True:
            if S[r] == "R":
                r -= 1
                ansr += 1
                while S[r] == "W":
                    if l == r:
                        print(max(ansl, ansr))
                        return
                    r -= 1
                break
            r -= 1
            if r == l:
                break
            
        if l == r:
            print(max(ansl, ansr))
            return
    
    


def main():
    n = getN()
    for _ in range(n):
        solve()


if __name__ == "__main__":
    
    solve()

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

Rcnt = C.count("R")
Ccnt = N - Rcnt
swapcnt = 0
for i in range(N):
    if i < Rcnt and C[i] == "W":
        swapcnt += 1

print(min(swapcnt, min(Rcnt, Ccnt)))

Solution (Python):
N = int(input())
c = list(input())
Wnum = c.count("W")
if Wnum == 0 or Wnum == N:
    print(0)
else:
    F = "R" * (N - Wnum) + "W" * Wnum
    t = 0
    for i in range(N):
        if c[i] != F[i]:
            t += 1
    print(t//2 + t%2)

Solution (Python):
N=int(input())
C=input()
ans=0
rc=C.count('R')
for i in range(rc):
    if C[i]=='W':
        ans+=1
print(ans)

Solution (Python):

import sys
input = lambda: sys.stdin.readline().strip()
n = int(input())
c = input()
ans = 0
for i in range(c.count('W')):
    ans += c[-(i + 1)] != 'W'
print(ans)

Solution (Python):
def main():
    N = int(input())
    c = str(input())
    c = list(c)
    
    i = -1
    j = N
    up = True
    ans = 0
    
    for n in range(N):
        if i == j:
            break
        if up:
            i += 1
            if c[i] == 'W':
                up = False
        else:
            j -= 1
            if c[j] == 'R':
                up = True
                ans += 1
            
    print(ans)
                
    
if __name__ == '__main__':
    main()

Solution (Python):



import collections
import sys
import copy
import re
import math


def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())


def main():
    N = I()
    C = S()
    rcount = 0
    ans = 0
    for c in C:
        if c == "R":
            rcount += 1

    for r in range(rcount, N):
        if C[r] =="R":
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N = read_int()
    C = list(input())
    Rs = []
    for i in range(N):
        if C[i] == 'R':
            Rs.append(i)
    count = 0
    for i in range(N):
        if C[i] == 'W':
            if len(Rs) == 0:
                break
            j = Rs.pop()
            if i < j:
                C[i], C[j] = C[j], C[i]
                count += 1
    return count


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

Solution (Python):
def solve():
    from collections import Counter
    n = int(input())
    s = input()
    c = Counter(s)
    x = c["R"]
    cnt = 0
    for i in range(x):
        if s[i] == "W":
            cnt += 1
    print(cnt)

solve()

Solution (Python):
n=int(input())
c=input()
s=0
t=n-1
ans=0
while s<t and s<n and t>=0:
    while c[s]=='R':
        s+=1
        if not s<n:
            break
    while c[t]=='W':
        t-=1
        if not t>=0:
            break
    if s<t:
        ans+=1
        s+=1
        t-=1
    else:
        break
print(ans)

Solution (Python):






def main():
    N = int(input())
    C = input()
    l = 0
    r = N-1
    count = 0
    while l < N and C[l] == 'R':
        l += 1
    while 0 <= r and C[r] == 'W':
        r -= 1
    while l < r:
        C = C[:l] + C[r] + C[l+1:r] + C[l] + C[r+1:]
        count += 1
        while l < N and C[l] == 'R':
            l += 1
        while 0 <= r and C[r] == 'W':
            r -= 1

    print(count)
    

main()

Solution (Python):
n=int(input())
c=input()
rlen=c.count("R")
wlen=c.count("W")
r=0
w=0
if rlen==0:
    print(0)
else:
    for i in c:
        if i=="R":
            r+=1
        else:
            w+=1
        if w==rlen-r:
            print(w)

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

r = 0
w = 0
for i in range(n):
    if(s[i] == "R"): r += 1
    else: w += 1

ngr = 0
ngw = 0
for i in range(n):
    if(i < r and s[i] == 'W'): ngw += 1
    if(i >= r and s[i] == 'R'): ngr += 1

print(min(ngw, ngr) + abs(ngw-ngr))

Solution (Python):
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")



def main():
    N = int(input())
    C = input()[:-1]
    
    m = C.count("R")
    r = 0
    w = 0
    for i in range(m):
        if C[i] == "W":
            w += 1
    for i in range(m, N):
        if C[i] == "R":
            r += 1
    
    print(max(r, w))


if __name__ == '__main__':
    main()

Solution (Python):
input()
s = input()
pref = 0
total = s.count('R')
res = int(1e9)
for i in range(total):
    pref += s[i] == 'R'
    res = min(res, total - pref)
print(res if total != len(s) and total != 0 else 0)

Solution (Python):
from sys import stdin, setrecursionlimit
from collections import Counter, deque, defaultdict
from heapq import heapify, heappush, heappop
from math import floor, ceil
from bisect import bisect_left
from itertools import combinations
setrecursionlimit(100000)

INF = int(1e10)
MOD = int(1e9 + 7)

def main():
    from builtins import int, map

    N = int(input())
    S = input()
    if 'W' not in S or 'R' not in S:
        print(0)
        exit()

    
    locR = [-i for i in range(N) if S[i] == 'R']
    locW = [i for i in range(N) if S[i] == 'W']
    heapify(locR)
    heapify(locW)

    dS = deque(S)
    
    
    count = 0
    while True:
        
        
        if (-locR[0]) < locW[0]:
            break
        
        right_most_R = heappop(locR)
        left_most_W = heappop(locW)
        heappush(locR, -left_most_W)
        heappush(locW, -right_most_R)
        count += 1
    print(count)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n = int(input())
    s = input()

    rc = s.count('R')
    rcl = s[:rc].count('R')

    print(rc - rcl)


if __name__ == '__main__':
    main()

Solution (Python):
import os
import heapq
import sys
import math
import operator
from collections import defaultdict
from io import BytesIO, IOBase


"""def gcd(a,b):
    if b==0:
        return a
    else:
        return gcd(b,a%b)"""

"""def pw(a,b):
    result=1
    while(b>0):
        if(b%2==1): result*=a
        a*=a
        b//=2
    return result"""



def inpt():
    return [int(k) for k in input().split()]


def main():
    n=int(input())
    s=list(input())
    ct=0
    i=0
    j=n-1
    while(i<=j):
        if(s[i]=='W'):
            while(i<j and s[j]=='W'):
                j-=1
            if(i==j):
                break
            ct+=1
            s[i],s[j]=s[j],s[i]
        i+=1
    for i in range(1,n):
        if(s[i-1]=='W' and s[i]=='R'):
            ct+=1
    print(ct)

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

if __name__ == "__main__":
    main()

Solution (Python):
N = int(input().rstrip())
c = list(input().rstrip()[:])
if c.count('W')==0:
    print(0)
else:
    print(c[-1*c.count('W'):].count('R'))

Solution (Python):
n = int(input())
s = input()
w_arr = [0]
r_arr = [0]

w_acc = 0
r_acc = 0
for i in range(n):
    if s[i] == 'W':
        w_acc += 1
    else:
        r_acc += 1

    w_arr.append(w_acc)
    r_arr.append(r_acc)


min_r = 999999999999
for i in range(n+1):
    w_1, r_1 = w_arr[i], r_arr[i]
    w_2, r_2 = w_arr[n] - w_arr[i], r_arr[n] - r_arr[i]

    if w_1 >= r_2:
        r = w_1
    else:
        r = r_2

    if r < min_r:
        min_r = r

print(min_r)

Solution (Python):
N = int(input())
li =input()
Rcount = 0
Wcount = 0

for i in range(N):
    if li[i] == "W":
        Wcount += 1
    else:
        Rcount += 1
anscount = 0

if Wcount >= Rcount:
    for i in range(Rcount):
        if li[i] == "W":
            anscount += 1
else:
    lire = li[::-1]

    for i in range(Wcount):
        if lire[i] == "R":
            anscount += 1
print(anscount)

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

Solution (Python):
import sys
def get_array(): return list(map(int , sys.stdin.readline().strip().split()))
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()

from collections import defaultdict 
import math 

n = int(input())
s = input()
w_cnt = 0
r_cnt = 0

for i in s:
	if i=='W':
		w_cnt+=1
	else:
		r_cnt+=1

if w_cnt ==0 or r_cnt == 0:
	print(0)
else:
	ans = 0
	for i in range(r_cnt):
		if s[i]=='W':
			ans+=1
	print(ans)

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

rcnt = C.count("R")
res = C[rcnt:].count("R")

print(res)

Solution (Python):
n=int(input())
c=input()
r=c.count("R")
r_1=c[:r].count("R")

ans=r-r_1
print(ans)

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

r = c.count('R')
ng = sum(map(lambda i: 1 if (i < r) == (c[i] == 'W') else 0, range(n)))
ans = ng // 2 + ng % 2
print(ans)

Solution (Python):
N = int(input())
c = list(input())
r = c.count('R')
w = c.count('W')

wrong_w = c[:r].count('W')
wrong_r = c[r:].count('R')

print(max(wrong_r,wrong_w))

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

l = 0
r = n - 1
count = 0

while True:
    while c[l] == "R" and l < r:
        l += 1
    while c[r] == "W" and l < r:
        r -= 1

    if r == l:
        break

    c[r], c[l] = c[l], c[r]

    count += 1

print(count)

Solution (Python):
N = int(input())
S = str(input())
wnum = 0
for i in range(N):
  if S[i] == "W":
    wnum += 1
right = 0
Flag = False
wright = 0
for i in range(N):
  
  if S[-1-i] == "R" and not Flag:
    right += 1
  elif S[-1-i] == "W":
      wright += 1
      Flag = True
  else:
    break
ans = 0
for i in range(wnum):
  if S[-1-i] != "W":
    ans += 1
print(ans)

Solution (Python):
import os
import sys
from io import BytesIO, IOBase






def main():
    n = int(input())
    s = input()
    rc = s.count('R')
    sor = 'R'*rc + 'W'*(n - rc)

    ed = 0
    for i in range(n):
        if s[i]!=sor[i]: ed += 1
    
    print(ed//2)


    
    
        





































BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

 
if __name__ == "__main__":
    main()

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

s = list(input())

l = 0
r = n-1

ans = 0

while l < r:
    if s[l] == 'W' and s[r] == 'R':
        s[l] = 'R'
        s[r] = 'W'
        ans += 1
        l += 1
        r -= 1
    else:
        if s[l] == 'R':
            l += 1
        if s[r] == 'W':
            r -= 1

print(ans)

Solution (Python):

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

Red_cnt = 0
for i in range(N):
    if C[i] == "R":
        Red_cnt += 1

ans = 0
for i in range(Red_cnt):
    if C[i] == "W":
        ans += 1
print(ans)

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

num_w = c.count("W")
num_r = N - num_w

comp = "R"*num_r + "W"*num_w
ans = 0
for i in range(num_r):
    if c[i] != comp[i]:
        ans += 1

ans = min(num_w, num_r, ans)
print(ans)

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

r=s.count('R')
ans=r

for i in range(r):
  if s[i]=='R':
    ans-=1
    
print(ans)

Solution (Python):
def do():
    if s.find("W") == -1:
        print(0)
        return
    c1 = s.count("W")
    c2 = s.count("R")
    c3 = 0
    l = 0
    r = len(s) - 1
    ll = len(s)
    while l < r and r >= 0 and l <= (ll-1):
        
        if s[l] == "R":
            l += 1
            continue
        if s[r] == "W":
            r -= 1
            continue
        
        c3 += 1
        l += 1
        r -= 1
    
    print(min(c1,c2,c3))


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

Solution (Python):
n=int(input())
s=list(input().strip())
i=0;j=n-1
an=0
while i<j:
  if s[i]=='W' and s[j]=='R':
    s[i]='R';s[j]='W'
    i+=1;j-=1
    an+=1;continue
  if s[i]=='R':
    i+=1
  if s[j]=='W':
    j-=1
print(an)

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

left = 0
right = N -1

ans = 0
while left < right:
    if C[left] == 'W' and C[right] == 'R':
        left += 1
        right -= 1
        ans += 1
    elif C[left] == 'W':
        right -= 1
    elif C[left] == 'R':
        left += 1
print(ans)

Solution (Python):
def main():
    N = int(input())
    S = input()
    R = S[::-1]
    cnt = 0
    i = 0
    r = N-1
    while i<r:
        if(S[i]=='W'):
            while i<r:
                if S[r]=='R':
                    cnt += 1
                    r -= 1 
                    break
                r -= 1
        i+=1

    print(cnt)


if __name__ == '__main__':
    main()

Solution (Python):
X=int(input())
t=input()
s=list(t)
co=0
re=0
a=s.count("R")
for i in range(X):
  if a==co:
    break
  elif s[i]=="W":
    re+=1
    co+=1
  else:
    co+=1
    
print(re)

Solution (Python):
import sys
import heapq

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


def main():
    N = int(input())
    C = list(input())

    Wcount=0
    Rcount =0
    rr =C.count("R")
    ww =C.count("W")


    for j in range(rr):
        if C[j]=="W":
            Rcount+=1

    for i in range(ww):
        if C[-1-i]=="R":
            Wcount+=1

    print(min(Rcount,Wcount))






if __name__ == "__main__":
    main()

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

ans = n
w_to_r = 0
r_to_w = s.count('R')

ans = min(ans, max(w_to_r, r_to_w))

for i in range(n):
    if s[i] == 'W':
        w_to_r += 1        
    else:
        r_to_w -= 1
    ans = min(ans, max(w_to_r, r_to_w))

print(ans)

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

num_r = moji.count('R')
num_w = moji.count('W')

ext_moji = moji[:num_r]
print(ext_moji.count("W"))

Solution (Python):









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

c_list = []
for i in range(N):
    if c[i] == "W":
        c_list.append(0)
    else:
        c_list.append(1)

n_R = sum(c_list)
c_left = c_list[:n_R]
c_right = c_list[n_R:]

W_left = len(c_left)-sum(c_left)
R_right = sum(c_right)

ans = max(W_left,R_right)

print(ans)

Solution (Python):
n = int(input())
s=input()
a=s.count('R')
l=0
for i in range(a):
    if s[i]=='R':
        l+=1
print(a-l)

Solution (Python):
import math




n=int(input())
c=input()
rall=c.count("R")
rnum=c[0:rall].count("W")
wnum=c[rall:-1].count("R")

print(max(rnum,wnum))

Solution (Python):
n = int(input())
C = list(input())
c = 0
pi = 0
pj = n-1
while True:
    for i in range(pi, n):
        if C[i] == 'W':
            break
    else:
        break
    for j in range(pj, 0, -1):
        if C[j] == 'R':
            break
    else:
        break
    if i < j:
        C[i] = 'R'
        C[j] = 'W'
        c += 1
    else:
        break
    pi = i
    pj = j
print(c)

Solution (Python):
N = int(input())
l = input()
arr = []
for i in range(N):
    arr.append(l[i])

c = 0
left = 0
right = N-1

T = False
for i in arr:
    if i == 'R':
        T = True
if T == False:
    print(c)
else:
    T = False
    for i in arr:
        if i == 'W':
            T = True
    if T == False:
        print(c)
    else:
        while True:
            while True:
                if arr[left] == 'R':
                    left += 1
                else:
                    break
            while True:
                if arr[right] == 'W':
                    right -= 1
                else:
                    break
            arr[left] = 'R'
            arr[right] = 'W'
            if left >= right:
                break
            else:
                c += 1
        print(c)

Solution (Python):
k = int(input())
a =input()

al = list(a)
alr = list(reversed(al))

from collections import Counter
count = Counter(al)
r = count["R"]
w = count["W"]

if r*w ==0:
    print(0)
    exit()

rf = al.index("R")
wf = al.index("W")

rfr =alr.index("R")
wfr =alr.index("W")


ans1 = r -wf

ans2 = w -rfr


alrr = al[:r]
countr = Counter(alrr)
rr = countr["R"]
ans3 = r - rr

print(min(ans1,ans2,ans3))

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

Rcnt = C.count('R')
Wcnt = C.count('W')
if len(C) == Rcnt or len(C) == Wcnt:
    print('0')
    exit()

print(Rcnt - C[:Rcnt].count('R'))

Solution (Python):
N = int(input())
A = list(input())
A = [1 if i == "W" else 0 for i in A]
W = sum(A)
R = N - W

print(sum(A[:R]))

Solution (Python):
I=lambda:list(map(int,input().split()))
n,=I()
s=list(input())
i=0
j=n-1
ans=0
while i<j:
	if s[i]=='R' and s[j]=='W':
		i+=1
		j-=1
	elif s[i]=='R':
		i+=1
	elif s[j]=='W':
		j-=1
	else:
		s[i]='R'
		s[j]='W'
		ans+=1
		i+=1
		j-=1
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
import math
from math import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations, chain, product
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):
    return fs[n] * invs[n-k] * invs[k] % modp



class UnionFindEx:
    def __init__(self, size):
        
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    
        self.roots[r2] = r1
        return True

Yes = 'Yes'
No = 'No'


from collections import deque

def main():
    N=input()
    C = input()
    W = C.count('W')
    R = C.count('R')
    RR = C[:R].count('W')
    WW = C[-W:].count('R')
    print(min(R, W, WW, RR))




main()

Solution (Python):
n=int(input())
s=input()
count=0
for i in range(n):
  if s[i]=='R':
    count+=1
for i in range(count):
  if s[i]=='R':
    count-=1
print(count)

Solution (Python):
n = int(input())
c = input()
r = 0
for i in c:
    if (i=="R"):
        r += 1

tnp = 0
for i in range(r):
    if (c[i]=="W"):
        tnp += 1
print(tnp)

Solution (Python):
n = int(input())
c = input()
wc = c.count("W")
rc = c.count("R")

print(min(rc - c[:rc].count("R"),wc,rc))

Solution (Python):


import math

import itertools
from collections import deque
from collections import Counter
import heapq
from fractions  import gcd


n=int(input())
c=list(input())
d=Counter(c)
w=d["w"]
r=d["R"]
res_w=res_r=0
for i in range(r):
    if c[i]=="W":
        res_w+=1
for i in range(r,n):
    if c[i] == "R":
        res_r+=1
print(min(res_r,res_w))

Solution (Python):
from collections import deque
n=int(input())
c=input()
r=deque()
lr=0
for i in range(n):
    if c[i]=="R":
        lr+=1
        r.append(i)
ans=0
for i in range(n):
    if lr==0:
        break
    if c[i]=="W":
        if i<r[-1]:
            ans+=1
            r.pop()
            lr-=1
        else:
            break

print(ans)

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

l = c[:c.count("R")]
print(l.count("W"))

Solution (Python):

import collections
from functools import lru_cache


def read():
    return input().strip()


def readInt():
    return int(input().strip())


def readList():
    return list(map(int, input().strip().split()))


def solve(N, arr):
    l = 0
    r = N-1

    ans = 0

    while l < r:
        while l < r and arr[l] != "W":
            l += 1

        while l < r and arr[r] != "R":
            r -= 1

        if l < r:
            ans += 1
            l += 1
            r -= 1

    return ans


N = readInt()
arr = [ch for ch in read()]

print(solve(N, arr))

Solution (Python):

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

w_pos = 0
r_pos = N - 1
w_flag = False
r_flag = False
ans = 0
for i in range(N):
    if w_pos >= r_pos:
        break
    if c[w_pos] == "W":
        w_flag = True
    else:
        w_flag = False
        w_pos += 1
    if c[r_pos] == "R":
        r_flag = True
    else:
        r_flag = False
        r_pos -= 1
    if w_flag and r_flag:
        ans += 1
        w_pos += 1
        r_pos -= 1

print(ans)

Solution (Python):
N = int(input())
c = list(input())
count = 0
i = 0
j = N-1
while i < j:
    while c[i] == "R" and i <= N-2:
        i += 1
    while c[j] == "W" and j >= 1:
        j -= 1
    if i < j:
        count += 1
    i += 1
    j -= 1
    if i >= j or i >= N or j <= 0:
        break
print(count)

Solution (Python):

from collections import Counter

n = int(input())

c = list(input())

C = Counter(c)

d = ['R']*C['R']+['W']*C['W']

ans = 0



for i in range(n):
    if c[i] != d[i]:
        ans += 1

print(ans//2)

Solution (Python):
n = int(input())
c = list(input())
n = c.count("R")
m = c[:n]
print(n-m.count("R"))

Solution (Python):
import math
def main():

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

    red = 0
    white = 0
    for i in range(N):
        if c[i] == "R": red += 1
        else: white += 1

    count = 0
    for i in range(red):
         if c[i] == "W": count += 1

    return count



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

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

A = str(0) + A
count = 0
for i in range(1,n+1):
    if A[i]=='R': count+=1

ans = 0
for j in range(count+1,n+1):
    if A[j]=='R': ans+=1
print(ans)

Solution (Python):
N = int(input())
c = input()
r, ans = 0, 0
for i in c:
    if i == 'R':
        r += 1

for i in range(r, N):
    if c[i] == 'R':
        ans += 1

print(ans)