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):
n = int(input())
c = list(input())
wc = 0

for i in range(n):
    if c[i] == "W":
        wc += 1

rc = n - wc

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

rinwcount = 0
for i in range(rc, n, 1):
    if c[i] == "R":
        rinwcount += 1

if winrcount >= rinwcount:
    print(winrcount)
else:
    print(rinwcount)

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

siro = s.count('W')
aka = n - siro
ans = 0
for i in range(aka, n):
    if s[i] == 'R':
        ans += 1
print(ans)

Solution (Python):
n = 0
c = []
ans = 0
"""
def changePosition(int stone1,int stone2):
    r = c[stone1]
    c[stone1] = c[stone2]
    c[stone2] = r
    ans += 1

def changeColor(int stone):
    if c[stone]=="R":
        c[stone]="W"
    else:
        c[stone]="R"
    ans += 1
"""

def main():
    n = int(input())
    c = input()

    red=c.count("R")
    num=c[0:red].count("R")
    print(red-num)


main()

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

nw = s.count("W")
nr = s.count("R")

change = s[nr:].count("R")

print(min(nw, nr, change))

Solution (Python):
n = int(input())
c = [i for i in input()]
cnt = 0
for num, i in enumerate(sorted(c)):
    if c[num] != i:
        cnt += 1
print(cnt // 2)

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

stones = list(input())

if not ('R' in stones and 'W' in stones):
    print('0')
    exit()
right = 0
left = N - 1
white_idx = -1
red_idx = -1
ans = 0
while right < left:
    while right < N and right < left:
        if stones[right] == 'W':
            white_idx = right
            break
        right += 1
    while 0 < left and right < left:
        if stones[left] == 'R':
            red_idx = left
            break
        left -= 1
    if left == right:
        break
    if 0 <= white_idx and 0 <= red_idx:
        ans += 1
    right += 1
    left -= 1
print(ans)

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

W_sum = stones.count('W')
R_sum = stones.count('R')

ans = min(W_sum, R_sum)
Wnum = 0
Rnum = 0
for i in range(n):
    if stones[i] == 'W':
        Wnum += 1
    else:
        Rnum += 1
    ans = min(ans, max(Wnum, R_sum - Rnum))

print(ans)

Solution (Python):
n=int(input())
arr=list(input())
w=arr.count('W')
r=arr.count('R')
ans=min(w,r)
leftr=r-arr[:r].count('R')
ans=min(ans,leftr)
print(ans)

Solution (Python):
n=int(input())
s=input()
i,j=0,len(s)-1
while(i<len(s) and s[i]=='R'):
    i+=1
while(j>=0 and s[j]=='W'):
    j-=1
c=0
while i<j:
    
    if s[i]=='W' and s[j]=='R':
        c+=1
        i+=1
        j-=1
        while i<len(s) and s[i]=='R':
            i+=1
        while(j>=0 and s[j]=='W'):
            j-=1
    
print(c)

Solution (Python):
n = int(input())
c = input()
n_r = c.count('R')
cnt = 0
for i in range(n_r):
    if c[i] == 'W':
        cnt += 1
print(cnt)

Solution (Python):
n=int(input())
ls=list(input())
r_ls = [i for i,v in enumerate(ls) if v=='R']
w_ls = [i for i,v in enumerate(ls) if v=='W']
end_flag = False
if len(r_ls) == 0 or len(w_ls) == 0:
    end_flag = True
else:
    r_mx, w_min = max(r_ls), min(w_ls)
    max_try = min(len(r_ls), len(w_ls))

r_i, w_i = -1, 0

if end_flag:
    print(0)
elif r_mx < w_min:
    print(0)
else:
    c = 0
    for i in range(max_try):
        c += 1
        r_ls[r_i], w_ls[w_i] =  w_ls[w_i], r_ls[r_i]
        if i == max_try -1:
            print(c)
            break
        else:
            try:
                r_mx = max(r_ls[r_i], r_ls[r_i - 1])
                w_min = min(w_ls[w_i], w_ls[w_i + 1])
            except:
                print(c)
                break
            if w_min > r_mx:
                print(c)
                break
            r_i -= 1
            w_i += 1

Solution (Python):
n=int(input())
c=list(input())
r=c.count('R')

cc=c[0:r]
ans=cc.count('W')

print(ans)

Solution (Python):

"""
Created on Sun Aug  2 21:11:34 2020

@author: NEC-PCuser
"""

N = int(input())

s = input()

l = 0
r = len(s) - 1
count = 0
while True:
	flg = False
	for i in range(l, r):
		if (s[i] == 'W'):
			l = i + 1
			flg = True
			break
	for j in range(r, l - 1, -1):
		if (s[j] == 'R'):
			r = j - 1
			if flg:
				count += 1
			break
	else:
		break
	if flg == False:
		break
print(count)

Solution (Python):

N = int(input())
s = list(input())

reds = 0
for c in s:
    reds += 1 if c == "R" else 0

whites = 0
for i in range(reds):
    whites += 1 if s[i] == "W" else 0

print(whites)

Solution (Python):
if __name__ == '__main__':
    n = int(input())
    c = input()

    r_count = c.count("R")
    w_count = c.count("W")

    diff = r_count - c[:r_count].count("R")
    print(diff)

Solution (Python):
from collections import deque

mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]

N = I()
c = input()
r_num = c.count("R")

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

Solution (Python):



import collections

def main():
    n = int(input())
    s = input()
    sc = collections.Counter(s)
    
    if sc['W'] == sc['R']:
        
        print(s[n//2:].count('R'))
        exit()
    print(s[:sc['R']].count('W'))


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


def input_list_str():
    return list(map(str, input().split()))


if __name__ == '__main__':
    main()

Solution (Python):

import math
from queue import PriorityQueue
import bisect
import copy
import time
from collections import Counter


n=int(input())
x=input()
x=list(x)
w=0
f=0
a=0
r=n-1
ans=min(x.count('W'),x.count('R'))
while w<=r:
	if x[w]=='W' and x[r]=='R':
		w+=1
		r-=1
		a+=1
	elif x[w]=='R':
		w+=1
	elif x[r]=='W':
		r-=1

print(min(a,ans))

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

r_cnt = c.count('R')
w_cnt = c.count('W')

cnt = 0

for i in range(r_cnt):
    if c[i] == 'R':
        continue
    else:
        cnt += 1

print(cnt)

Solution (Python):



from math import *
from copy import *                      
from heapq import *                     
from string import *                    
from random import *                    
from bisect import *                    
from sys import stdin                   
from sys import maxsize                 
from operator import *                  
from itertools import *                 
from decimal import Decimal             
from collections import Counter         
from collections import defaultdict     

'''
8
WRWWRWRR
'''

def solve(l):
	l = list(l)
	n = len(l)
	if(len(set(l)) == 1):
		return 0
	m1 = l.count('R')
	m2,m3,m4,m5 = 0,0,l.count('W'),l.count('R')
	f = 0
	for i in range(n):
		if(f and l[i] == 'R'):
			m2 += 1
		elif(l[i] == 'W'):
			f = 1
	reds = l.count('R')
	rs = 0
	for i in range(reds):
		if(l[i] == 'R'):
			rs += 1
	m3 = reds - rs
	
	return min(m1,m2,m3,m4,m5)
	

N = int(stdin.readline())
l = input()
ans = solve(l)
print(ans)

Solution (Python):
N = int(input())
s = input()
fir = True
f = 0
b = len(s)-1
count = 0
while(b > f):
    if(s[f] == "R"):
        f += 1
        continue
    elif(s[b] == "R"):
        count += 1
        f += 1
        b -= 1
        continue
    elif(s[b] == "W"):
        b -= 1
print(count)

Solution (Python):
N = int(input())
C = input()
Nw = C.count('W')
Nr = N - Nw
print(Nw - C[(-1*Nw):].count('W'))

Solution (Python):
def resolve():
    N = int(input())
    C = list(input())
    rnum = 0
    for c in C:
        if c == "R":
            rnum += 1
    leftr = 0
    for c in C[:rnum]:
        if c == "R":
            leftr += 1
    print(rnum-leftr)
    

if '__main__' == __name__:
    resolve()

Solution (Python):

import sys
input = sys.stdin.readline


def ceil(x):
    if x != int(x):
        x = int(x) + 1;
    return x;
    

def swaparr(arr, a,b):
    temp = arr[a];
    arr[a] = arr[b];
    arr[b] = temp;


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


def nCr(n, k): 
    if(k > n - k): 
        k = n - k; 
    res = 1;
    for i in range(k): 
        res = res * (n - i);
        res = res / (i + 1); 
    return int(res);


def upper_bound(a, x, lo=0, hi = None):
    if hi == None:
        hi = len(a);
    while lo < hi:
        mid = (lo+hi)//2;
        if a[mid] < x:
            lo = mid+1;
        else:
            hi = mid;
    return lo;


def primefs(n):
    
    primes = {}
    while(n%2 == 0 and n > 0):
        primes[2] = primes.get(2, 0) + 1
        n = n//2
    for i in range(3, int(n**0.5)+2, 2):
        while(n%i == 0 and n > 0):
            primes[i] = primes.get(i, 0) + 1
            n = n//i
    if n > 2:
        primes[n] = primes.get(n, 0) + 1
    
    
    return primes


def power(x, y, p): 
    res = 1
    x = x % p  
    if (x == 0) : 
        return 0
    while (y > 0) : 
        if ((y & 1) == 1) : 
            res = (res * x) % p 
        y = y >> 1      
        x = (x * x) % p 
    return res 


def swap(a,b):
    temp = a
    a = b
    b = temp
    return a,b;









def find(x, link):
    p = x;
    while( p != link[p]):
        p = link[p];
    
    while( x != p):
        nex = link[x];
        link[x] = p;
        x = nex;
    return p;




def union(x, y, link, size):
    x = find(x, link)
    y = find(y, link)
    if size[x] < size[y]:
        x,y = swap(x,y)
    if x != y:
        size[x] += size[y]
        link[y] = x


def sieve(n): 
    prime = [True for i in range(n+1)] 
    p = 2
    while (p * p <= n): 
        if (prime[p] == True): 
            for i in range(p * p, n+1, p):
                prime[i] = False
        p += 1
    return prime


MAXN = int(1e5 + 5)
def spf_sieve():
    spf[1] = 1;
    for i in range(2, MAXN):
        spf[i] = i;
    for i in range(4, MAXN, 2):
        spf[i] = 2;
    for i in range(3, ceil(MAXN ** 0.5), 2):
        if spf[i] == i:
            for j in range(i*i, MAXN, i):
                if spf[j] == j:
                    spf[j] = i;
    




def factoriazation(x):
    ret = {};
    while x != 1:
        ret[spf[x]] = ret.get(spf[x], 0) + 1;
        x = x//spf[x]
    return ret;
    
    


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

def float_array():
    return list(map(float, input().strip().split()));


def str_array():
    return input().strip().split();


MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');








n = int(input()); s = input().strip();
ans = min(s.count('R'), s.count('W'));

w = s.count('W'); cnt = 0;
for i in range(n - w):
    if s[i] == 'W':
        cnt += 1;
r = s.count('R');

if cnt <= r:
    ans2 = cnt;
else:
    ans2 = cnt*2 - r;
ans = min(ans2, ans);
print(ans);

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

r, w = 0, 0
for i in range(N):
    if S[i] == 'R':
        r += 1
    else:
        w += 1

res = [0 for _ in range(N+1)]
for i in range(1, N+1):
    if S[i-1] == 'R':
        res[i] = res[i-1] + 1
    else:
        res[i] = res[i-1]

print(r-res[r])

Solution (Python):
import sys
from io import StringIO
import unittest
import os


sys.setrecursionlimit(999999999)



def resolve(test_def_name=""):
    n = int(input())
    rw_s = list(input())

    count = rw_s.count("R")
    acted = rw_s[0:count].count("R")

    print(count - acted)



class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """4
WWRR"""
        output = """2"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """2
RR"""
        output = """0"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """8
WRWWRWRR"""
        output = """3"""
        self.assertIO(test_input, output)

    
    def tes_t_1original_1(self):
        test_input = """データ"""
        output = """データ"""
        self.assertIO(test_input, output)



if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        
        resolve()

    else:
        
        unittest.main()

Solution (Python):
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    
    n = int(input())
    c = input()

    s = 0
    e = n-1
    cnt = 0
    while s < e:
        while c[s] != 'W' and s < e:
            s+= 1
        
        while c[e] != 'R' and s < e:
            e -= 1
        if c[e] != c[s]:
            cnt += 1
        s+= 1
        e -= 1
    print(cnt)

        

if __name__ == '__main__':
    main()

Solution (Python):
def _run(N, colors):
    count = 0
    head_i = 0
    tail_i = N - 1
    while head_i < tail_i:
        if colors[head_i] == "W" and colors[tail_i] == "R":
            count += 1
            head_i += 1
            tail_i -= 1
        else:
            if colors[head_i] != "W":
                head_i += 1
            if colors[tail_i] != "R":
                tail_i -= 1

    print(count)


def _main():
    N = int(input())
    colors = str(input())
    _run(N, colors)


if __name__ == "__main__":
    _main()

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

myr_cnt = c.count('R')
if myr_cnt == 0:
    print(0)
    sys.exit()

myw_cnt = 0
for i in range(myr_cnt):
    if c[i] == 'W':
        myw_cnt += 1
print(myw_cnt)

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

r = C.count("R")
ms = C[:r].count("R")

print(r - ms)

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

cnt_r = c.count("R")

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

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

C_list = list(C)

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

tmp = ['R']*R_count + ['W']*W_count

ans = 0

for i in range(N):
    if C_list[i] != tmp[i]:
        ans += 1
        
print(ans//2)

Solution (Python):
N = int(input())
C = str(input())
count = C.count("R")
ans = 0

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

print(ans)

Solution (Python):
if __name__ == '__main__':
	N = int(input())
	S = input()
	L = 0
	R = N-1
	count = 0
	if S.count('W') == 0 or S.count('R') == 0:
		R = -1
	while(L < R):
		while(S[L] == 'R'):
			L += 1
		while(S[R] == 'W'):
			R -= 1
		if L > R:
			break
		L += 1
		R -= 1
		count += 1

	print(count)

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from collections import deque
from decimal import *
import queue

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007




def main():
	N = inputInt()
	C = inputStr()

	aida = []
	anzen = 0
	ai = 0
	yokei = 0
	flg = False
	for i,val in enumerate(C):
		ai += 1
		if val == "W":
			if flg == False:
				flg = True
		elif val == "R":
			anzen += 1
			if flg == False:
				aida.append(ai)
			else:
				yokei += 1
				aida.append(ai)

	if yokei == 0:
		print(0)
		sys.exit()

	ans = 0
	for i,val in enumerate(aida):
		if val <= anzen:
			continue
		ans += 1

	print(ans)
	
	




if __name__ == "__main__":
	main()

Solution (Python):


def solve():
    N = int(input())
    Cs = input()

    ans = 0
    left, right = 0, N-1
    while left <= right:
        if Cs[left]=="W" and Cs[right]=="R":
            ans += 1  
            left, right = left+1, right-1
            continue
        elif Cs[left]=="W" and Cs[right]=="W":
            right -= 1
            continue
        elif Cs[left]=="R" and Cs[right]=="R":
            left += 1
            continue
        else:
            left, right = left+1, right-1
            continue


    print(ans)


if __name__ == "__main__":
    solve()

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

l = 0
r = n-1
ans = 0
while(True):
    while(s[l]=='R' and l<n-1):
        l+=1
    while(s[r]=='W' and 0<r):
        r-=1
    if l < r:
        ans += 1
        l += 1
        r -= 1
    else:
        break

print(ans)

Solution (Python):
n = int(input())
c = input()
s2 = "".join(sorted(list(c)))
ans = 0
for i in range(n):
    ans += 1 if c[i] != s2[i] else 0
print(ans // 2)

Solution (Python):
n=int(input())
if n==0:
	exit()
st=input()
r=st.count("R")
print(st[:r].count("W"))

Solution (Python):
N = int(input())
c = list(input())
sousa = 0
w = 0
r = N-1

for i in range(N):
    if c[w] == 'W' and c[r] == 'R':
        c[w] = 'R'
        c[r] = 'W'
        r -= 1
        w += 1
        sousa += 1
    elif c[w] == 'W':
        r -= 1
    elif c[r] == 'R':
        w += 1
    else:
        r -= 1
        w += 1
    if w >= r:
        break

print(sousa)

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

def solve(s,n):
    if "W" not in s or "R" not in s or s.find("WR")==-1:
        return 0

    else:
        arr=list(s)
        i=0
        j=n-1
        count=0

        while i<j:
            if arr[i]=="W" and arr[j]=="R":
                count += 1
                i+=1
                j-=1

            elif arr[i]=="W" and arr[j]=="W":
                j-=1
            elif arr[i]=="R" and arr[j]=="R":

                i+=1
            else:
                i+=1
                j-=1
        return count
print(solve(s,n))

Solution (Python):
N = int(input())
c = list(input())
R = 0
for i in c:
    if i == 'R':
        R +=1
r = 0
for i in range(0,R):
    if c[i] == 'R':
        r +=1
print(R-r)

Solution (Python):
n = int(input())
c = input()
w = 0
for i in range(c.count("R")):
    if c[i] == "W":
        w += 1
print(w)

Solution (Python):
n=int(input())
c=input()
a=c.count("R")
d=c[:a]
b=d.count("R")
print(a-b)

Solution (Python):
from heapq import heapify, heappush, heappop

def main(C):
    R = []
    W = []
    for i, c in enumerate(C):
        if c == 'R':
            R.append(-i)
        else:
            W.append(i)
    if len(R)==0 or len(W)==0:
        return 0
    heapify(R)  
    heapify(W)
    cnt = 0
    while True:
        
        r_last = -heappop(R)
        w_first = heappop(W)
        if r_last < w_first:
            break
        heappush(R, -w_first)
        heappush(W, r_last)
        cnt += 1
    return cnt
    

if __name__ == "__main__":
    N = int(input())
    C = input()
    ans = main(C)
    print(ans)

Solution (Python):
N,c = open(0).read().split()
N = int(N)
white_right = 0
red = 0
red_left = 0
for x in c:
    if x == 'R':
        red_left += 1
    else:
        break
c_altered = c[red_left:]
l = len(c_altered)
for x in c_altered:
    if x == 'R':
        red += 1
        white_right = 0
    else:
        white_right += 1
c_altered = c_altered[:l-white_right]
l = len(c_altered)
ans1 = red
ans2 = c_altered[:red].count('W')
print(min(ans1,ans2))

Solution (Python):
n = int(input())
s = input()
s = list(map(lambda x: 1 if x == 'R' else 0,s))
ans = min(s.count(1), s.count(0))
idx = s.count(1)
ans = min(ans,idx - s[:idx].count(1))
print(ans);

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


sW = 0
sR = 0
fW = N
fR = N-1
res = 0

while True:
  for i in range(sW,N):
    if S[i]=="W":
      fW = i
      break
  for i in range(sR,N):
    if S[N-i-1]=="R":
      fR = i
      break
  if fW<N-fR-1:
    S[fW] = "R"
    S[N-fR-1] = "W"
    sW = fW
    sR = fR
    res += 1
  else:
    break
    
print(res)

Solution (Python):
from collections import deque

N = int(input())
C = deque(list(input()))

cnt = 0

while len(C) > 1:
    if C[0] == 'W' and C[-1] == 'R':
        cnt += 1
        C.popleft()
        C.pop()
    else:
        if C[0] == 'R':
            C.popleft()
        if C[-1] == 'W':
            C.pop()

print(cnt)

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

rcount = c.count("R")
WcountuntilR = c[0:rcount].count("W")
print(WcountuntilR)

Solution (Python):
N = int(input())
l = input()
k = [[0,0] for i in range(N)]
Rc = 0
Wc = 0
for i in range(N):
    if l[i]=='R':
        Rc += 1
    else:
        Wc += 1

for i in range(N-1):
    k[i+1][0]=k[i][0]
    if l[i]=='W':
        k[i+1][0]+=1
    k[i][1]=Rc+k[i+1][0]-(i+1)

ans = 3*10**5
for i in range(N):
    if ans >= max(k[i][0],k[i][1]):
        ans = max(k[i][0],k[i][1])


print(ans)

Solution (Python):
import sys
import  math
from heapq import *
import fractions
import bisect
stdin = sys.stdin
     
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N=int(input())
c=list(ns())
ans=0
w_l=[]
r_l=[]
for i in range(len(c)):
    if(c[i]=='W'):
        w_l.append(i)
    else:
        r_l.append(i)
    
w_l.sort()
r_l.sort(reverse=True)

for i in range(min(len(w_l),len(r_l))):
    if(w_l[i]<r_l[i]):
        ans+=1
    
print(ans)

Solution (Python):
N = int(input())
A = input()
Ar = A[::-1]
i = 0
j = 0
res = 0
while True:
    x = A[i:-1].find("W")
    y = Ar[j:-1].find("R")
    if x == -1 or y == -1:
        break
    i += x
    j += y
    if i+j >= N:
        break
    else:
        res += 1
    i+= 1
    j+= 1
print(res)

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

from collections import deque

red = deque()
white = deque()
for i, ci in enumerate(c):
    if ci == "R":
        red.append(i)
    else:
        white.append(i)
if len(red) == 0:
    print(0)
    exit()
if len(white) == 0:
    print(0)
    exit()

red.appendleft(-1)
white.append(2000000)

cnt = 0
while True:
    r = red.pop()
    w = white.popleft()
    if w > r:
        print(cnt)
        break
    cnt += 1

Solution (Python):
n=int(input())
cc=input()
rcount=0
wcount=0
count=0
for c in cc:
  if c=="R":
    rcount+=1
wcount=n-rcount
for i in range(rcount):
  if cc[i]=="W":
    count+=1
print(count)

Solution (Python):
def main():
    N = int(input())
    c = [x == 'R' for x in input().rstrip()]
    
    left = 0
    right = N - 1
    count = 0
    while left < right:
        while left < N and c[left] != False:
            left += 1
        while right >= 0 and c[right] != True:
            right -= 1
        if left < right:
            count += 1
            left += 1
            right -= 1
    print(count)

if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
c = input()
new_str_list = sorted(c)
C = ''.join(new_str_list)

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

Solution (Python):
n = int(input())
C = list(input())
count_r = C.count("R")
count_r_right = C[:count_r].count("R")
print(min(count_r-count_r_right,C.count("W")))

Solution (Python):
N=int(input())
stones=list(input())
reds,whites=[0,0]
counter=0
for i in stones:
	if i=='R':
		reds+=1
	else:
		whites+=1
for i in range(N-whites,N):
	if stones[i]=='W':
		counter+=1
print(whites-counter)

Solution (Python):
n = int(input())
s = input()
w = []
r = []
for i,j in enumerate(s):
  if j == 'W':
    w.append(i)
  else:
    r.append(i)
if len(w) == 0 or len(r) == 0:
  print(0)
else:
  p = -1
  q = 0
  ans = 0
  mp = len(r)
  mq = len(w)
  while r[p] > w[q]:
    ans += 1
    p -= 1
    q += 1
    if q >= mq or -p > mp:
      break
  print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
from collections import Counter
def main():
    N = int(input())
    C = list(input().rstrip())
    D = Counter(C)
    E = D['R']
    CNT = 0
    for i in range(E):
        if C[i] == 'W':
            CNT += 1
    print(CNT)

if __name__ == '__main__':
    main()

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

count_r = c.count('R')
count = 0

for i in range(N):
	if count_r == 0: break
	if c[i] == 'W':
		count += 1
		count_r -= 1
	if c[i] == 'R':
		count_r -= 1

print(count)

Solution (Python):

import numpy as np

n = int(input())
s = np.array(list(map(lambda x: 1 if x == "W" else 0,
                      list(input()))), 
             dtype=int)

wp = np.cumsum(s)
ope = 0
alter = 0
for c, w in zip(s[::-1], wp[::-1]):
    
    if c == 1:
        continue

    
    if w - ope > 0:
        ope += 1
    
    if w - ope == 0:
        break

print(ope)

Solution (Python):
N = int(input())
C = list(map(str, input()))

W = N - C.count('W')
R = N - C.count('R')

CH = W - C[:W].count('R')
print(min(W, R, CH))

Solution (Python):
n = int(input())
s = input()
start = 0
end = n-1
count = [[0, 0] for _ in range(n)]
start_w = 0
end_r = 0
for i in range(n):
    count[i][0] = start_w
    if s[i] == "W":
        start_w += 1
for i in reversed(range(n)):
    count[i][1] = end_r
    if s[i] == "R":
        end_r += 1

print(min(map(max,count)))

Solution (Python):
n = int(input())
s = list(input())
j = 1
count = 0
for i in range(n):
    if s[i] == 'R':
        continue
    else:
        while 1:
            if j + i >= n:
                break
            elif s[-j] == 'R':
                s[i] = 'R'
                s[-j] = 'W'
                count += 1
                break
            j += 1
print(count)

Solution (Python):
n=int(input())
c=str(input())
r=0
w=0
for i in range (n):
    if c[i]=='R':
        r+=1
    else:
        w+=1
q=r
for j in range (r):
    if c[j]=='R':
        q-=1
print(q)

Solution (Python):
n = int(input())
c_l = list(input())
c_l = [ 1  if c == 'R' else 0 for c in c_l ]
r_n = sum(c_l)
w_n = sum(c_l[:r_n])
print(r_n-w_n)

Solution (Python):
from collections import deque

N = int(input())
c = list(input())
w_index = deque([i for i, x in enumerate(c) if x == 'W'])
r_index = deque([i for i, x in enumerate(c) if x == 'R'])

if len(w_index) == 0 or len(r_index) == 0:
    print(0)
    exit()

ans = 0

while w_index[0] < r_index[-1]:
    l = w_index.popleft()
    r = r_index.pop()
    w_index.append(r)
    r_index.appendleft(l)
    ans += 1
print(ans)

Solution (Python):
n=int(input())
s=list(input())
i=0
j=n-1
ans=0
while i<j:
    while s[i]!="W" and i<j:
        i+=1
    while s[j]!="R" and i<j:
        j-=1
    if i<j:
        ans+=1
        i+=1
        j-=1
print(ans)

Solution (Python):
n = int(input())
c = input()
numr = c.count("R")
a = "R"*numr + "W"*(n-numr)
ans = 0
for i in range(n):
  if c[i] != a[i]:
    ans += 1
print(ans//2)

Solution (Python):
N = int(input())
C = input()
ALL_R = "R" * len(C)
ALL_W = "W" * len(C)
RW = "R" * C.count("R") + "W" * C.count("W")
cnt1 = 0
cnt2 = 0
cnt3 = 0
for i in range(len(C)):
    if C[i] != ALL_R[i]:
        cnt1 += 1
    if C[i] != ALL_W[i]:
        cnt2 += 1
    if C[i] != RW[i]:
        cnt3 += 1
cnt3 //= 2
print(min(cnt1, cnt2, cnt3))

Solution (Python):
N = int(input())
l = list(input())
w = [0]*N
if l[0] == "W":
    w[0] = 1
for i in range(1,N):
    if l[i] == "W":
        w[i] = w[i-1] + 1
    else:
        w[i] = w[i-1]
kosuu = N - w[N-1]
if kosuu == 0:
    print(0)
    exit()
print(w[kosuu-1])

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

white_position = []
red_potition = []
for i in range(n):
    if li[i] == 'W':
        white_position.append(i)
    else:
        red_potition.append(i)
        
red_potition.sort(reverse=True)

cnt = 0
for s,t in zip(white_position, red_potition):
    if s < t:
        li[s] = 'R'
        li[t] = 'W'
        cnt += 1
        
check = False
for i in range(n):
    if li[i] == 'W':
        check = True
    if check == True and li[i] == 'R':
        cnt += 1

print(cnt)

Solution (Python):

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


print(c.count("R")-c[:c.count("R")].count("R"))

Solution (Python):
import sys

def input(): return sys.stdin.readline().rstrip("\r\n")
def List(): return list(map(int, input().split()))
def Num(): return int(input())


n = Num()
s = input()
r, w = s.count('R'), s.count('W')
t = 'R' * r + 'W' * w
f, ss = 0, 0
for i in range(r):
    if t[i] != s[i]:
        f += 1
print(f)

Solution (Python):
n=int(input())
s=input()
nr=s.count("R")
nw=s[:nr].count("W")
print(nw)

Solution (Python):
from collections import Counter
n = int(input())
c = list(input())
for i in range(n)[-1::-1]:
    if c[i] == "W":
        del c[i]
    else:
        break
c.reverse()
for i in range(len(c))[-1::-1]:
    if c[i] == "R":
        del c[i]
    else:
        break

d = Counter(c)
r = d["R"]
w = d["W"]
rc, wc = 0, 0
if w > r:
    c.reverse()
    for i in range(r):
        if c[i] == "R":
            rc += 1
    print(r - rc)
else:
    for i in range(w):
        if c[i] == "W":
            wc += 1
    print(w - wc)

Solution (Python):
from collections import deque
import bisect

N = int(input())

S = input()

Ws = deque([])
Rs = deque([])

for i in range(N):
  if S[i] == 'W':
    Ws.append(i)
  else:
    Rs.append(i)
    
cnt = 0
for i in range(len(Rs)-1,-1,-1):
  j = bisect.bisect_left(Ws, Rs[i])
  if j > 0:
    Ws.popleft()
    cnt += 1
    
print(cnt)

Solution (Python):
n=int(input())
c=input()
R=c.count("R")
W=c.count("W")
LW=0
for i in range(R):
    if c[i]=="W":
        LW+=1
print(LW)

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

ans = 0

R = c.count('R')
W = c.count('W')
hoge = 'R' * R + 'W' * W

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

print(ans // 2)

Solution (Python):
n = int(input())
s = input()
nw = len(s.replace('R',''))
nr = n - nw

if (nr ==0) or (nw==0):
    print(0)
else:
    tmp_w = 0
    ret = n
    w_left = 0
    for i, c in enumerate(s):
        if c == 'W':
            w_left += 1
        r_right = nr + w_left - i - 1
        ret = min(ret, max(r_right, w_left))
    print(ret)

Solution (Python):
K = int(input())
C = input()
C = list(C)
r = C.count("R")
A = C[:r].count("W")
B = C[-K+r:].count("W")


cnt = 0
for _ in range(200000):
    if A > 0:
        if B > 0:
            A -= 1
            B += 1
            cnt += 1
        else:
            A -= 1
            cnt += 1
    else:
        break
print(cnt)