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):
if __name__ == '__main__':
    N = int(input())
    C = list(input())
    cnt = 0
    red_cnt = 0
    for c in C:
        if c == 'R':
            red_cnt += 1

    for i in range(red_cnt):
        if C[i] == 'W':
            for j in range(red_cnt, N):
                if C[j] == 'R':
                    C[i] = 'R'
                    C[j] = 'W'
                    cnt += 1
                    red_cnt = j+1
    print(cnt)

Solution (Python):
import heapq

def main():
    N = int(input())
    C = list(input())
    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:
        print(0)
        exit()
    heapq.heapify(R)
    heapq.heapify(W)
    ans = 0
    while True:
        max_r = -1 * heapq.heappop(R)
        min_w = heapq.heappop(W)
        if max_r < min_w:
            break
        else:
            heapq.heappush(R, -min_w)
            heapq.heappush(W, max_r)
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
S=input()
ls=[0 for _ in range(N)]
for i in range(N):
  if S[i]=='W':
    ls[i]=1
ans=0
i=0
j=N-1
while True:
  if ls[i]==1 and ls[j]==0:
    ls[i]=0
    ls[j]=1
    i+=1
    j-=1
    ans+=1
  elif ls[i]==0 and ls[j]==0:
    i+=1
  elif ls[i]==1 and ls[j]==1:
    j-=1
  else:
    i+=1
    j-=1
  if i>=j:
    break
print(ans)

Solution (Python):
n = int(input())
s = list(input().rstrip())

left_cur = 0
right_cur = n - 1
ans = 0
flag = False
while(left_cur < right_cur and left_cur < n - 1 and right_cur > 0):
    while(s[left_cur] == 'R'):
        left_cur += 1
        if (left_cur == n - 1):
            flag = True
            break
    while(s[right_cur] == 'W'):
        right_cur -= 1
        if (right_cur == 0):
            flag = True
            break
    if(left_cur > right_cur):
        flag = True
    if(flag):
        break
    ans += 1
    left_cur += 1
    right_cur -= 1

print(ans)

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

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

count = 0

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

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

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

Solution (Python):
N = int(input())
S = input()
Answer = 0
White = 0
Red = S.count('R')
for j in range(len(S)):
    i = S[j]
    if i == 'W':
        White += 1
    elif i == 'R' and White >0 and j+1 > Red:
        Answer += 1
        White -= 1
print(Answer)

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

Solution (Python):
n=int(input())
s=input()
w=s.count('W')
r=s.count('R')

ans=10**9
if w==0 or r==0:
  print(0)
  exit(0)

left_white, right_red=0,r
for i in range(n):
  if s[i]=='W':
    left_white+=1
  else:
    right_red-=1
  ans=min(ans,max(left_white,right_red))
  
print(ans)

Solution (Python):
import sys
read = sys.stdin.read

def main():
    n = int(input())
    rc = tuple(input())
    rc2 = [c == 'W' for c in rc]
    r1 = sum(rc2) 
    r2 = n - r1  
    r3 = sum(rc2[:r2]) 
    print(min(r1, r2, r3))

if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
def main():
    N = int(readline())
    chars = readline().decode()

    cntW = [0]
    cntR = [0] 
    for i in range(N):
        cntW.append(cntW[i] + (1 if chars[i] == 'W' else 0))
        cntR.append(cntR[i] + (1 if chars[i] == 'R' else 0))

    ans = int(1e9)
    for i in range(N+1):
        leftW = cntW[i]
        rightR = cntR[N]-cntR[i] 

        if leftW <= rightR:
            tmp = leftW + (rightR - leftW)
        else:
            tmp = rightR + (leftW - rightR)
        ans = min(tmp, ans)
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
def abc174d():
    n = int(input())
    s = input()
    cnt = s.count('R')
    if cnt == len(s):
        print(0)
        return
    start = 0
    while s[start] == 'R':
        start += 1
        cnt -= 1
    ans = cnt
    for i in range(start, start+cnt):
        if s[i] == 'R':
            ans -= 1
    print(ans)
abc174d()

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

r = c.count("R")
ans = 0
for i in range(r):
  if c[i] == "W": ans += 1
print(ans)

Solution (Python):

n = int(input())
c = input()

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

for i in range(R):
    if c[i]=="R":
        cnt += 1
print(R-cnt)

Solution (Python):
from sys import stdin

input = stdin.readline
import math

def solve():
    n = int(input())
    a = list(input().strip())
    left = 0
    rpos = [i for i in range(n) if a[i] == 'R']
    if not rpos:
        print(0)
        return
    right = len(rpos) - 1
    cnt = 0
    while right >= 0 and left < rpos[right]:
        if a[left] == 'W':
            cnt += 1
            right -= 1
        left += 1
    print(cnt)

if __name__ == '__main__':
    solve()

Solution (Python):
import bisect

N = int(input())
c = list(input())
c_sorted = sorted(c)

R_count = bisect.bisect_right(c_sorted, 'R')
ans = 0
for i in range(R_count):
    if c[i] != 'R':
        ans += 1
print(ans)

Solution (Python):
if __name__ == '__main__':
    n = int(input())
    s = list(input())
    
    
    ss = sorted(s)
    
    cnt = 0
    
    if "W" in s:
        sind = ss.index("W")

        
        cnt = 0
        for i in range(sind):
            if s[i] == "W":
                cnt += 1

    print(cnt)

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

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

print(ans)

Solution (Python):
n=int(input())
s = str(input())
r = 0
w = 0
d = 0
for i in range(n):
    if (s[i] == 'R'):
        r += 1
    else:
        w += 1
sd = sorted(s)
for i in range(n):
    if (s[i] != sd[i]):
        d += 1

if (w == 0 or r == 0):
    print(0)
else:
    print(min(r, w, int(d/2)))

Solution (Python):
from collections import Counter

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

cnt = Counter(C)
ans1 = min(cnt["R"], cnt["W"])

ans2 = 0
l, r = 0, N-1
while l < r:
    if C[l] == "W" and C[r] == "R":
        ans2 += 1
        l += 1
        r -= 1
    else:
        if C[l] == "R":
            l += 1
        if C[r] == "W":
            r -= 1

ans = min(ans1, ans2)
print(ans)

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

def input(): return sys.stdin.readline()[:-1]
mod = 10**9 + 7
def readInt():return int(input())
def readIntList():return list(map(int,input().split()))
def readStringList():return list(input())
def readStringListWithSpace():return list(input().split())
def readString():return input()

n = readInt()
text = readStringList()
import collections
c  = collections.Counter(text)
if c['W'] == 0:
    print("0")
    exit()
i,j,count = 0,len(text)-1,0

while i <= j and i < len(text):
    if text[i] == 'W':
        while text[j] != 'R' and j > 0:
            j -= 1
        if i <= j and j > 0:
            text[i],text[j] = text[j],text[i]
            count += 1
    i += 1
print(count)

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

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

if cl[:cl.count('R')].count('W') != 0:
    print(min(cl[:cl.count('R')].count('W'), cl.count('R')))
else:
    print(0)

Solution (Python):
N=int(input())
S=input()
i=0
j=N-1
ans=0
while True:
    while S[j]=='W':
        j-=1
        if j<0:
            break
    while S[i]=='R':
        i+=1
        if i>=N:
            break
    if j<i:
        break
    ans+=1
    i+=1
    j-=1
    if j<i:
        break
print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(3*10**5)

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

def search(l,r):
    
    if l==r:
        return 0
    if S[l]=='R':
        return search(l+1,r)
    if S[r-1]=='W':
        return search(l,r-1)
    return search(l+1,r-1)+1
print(search(0,N))

Solution (Python):
n = int(input())
c = list(input())
w = 0
r = c.count("R")
ans = r
for i in range(n) :
    if c[i] == "W" :
        w += 1
    else :
        r -= 1
    if ans > max(w,r) :
        ans = max(w,r)

print(ans)

Solution (Python):
n = int(input())
s = input()
res = 0
tem = s.find('WR')
if tem == -1:
    print(res)
else:
    i, j = 0, len(s)-1
    while i<j:
        while i < j and s[i] != 'W': i+=1
        while i < j and s[j] != 'R':j -= 1
        if s[i] == 'W' and s[j] == 'R':
            res += 1
            i += 1
            j -= 1
    print(res)

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

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

Solution (Python):
from collections import Counter
N = int(input())
s = input()
c = Counter(s)
w, r = 0, c['R']
ans = max(w, r)
for i in range(N):
    if s[i] == 'W': w += 1
    else: r -= 1 
    ans = min(ans, max(w, r))

print(ans)

Solution (Python):
n=int(input())
c=list(input())
r=[]
w=[]
for i in range(n):
  if c[i]=='R':
    r.append(i)
  else:
    w.append(i)
r.reverse()
ans=0
for i in range(min(len(r),len(w))):
  if r[i]>w[i]:
    ans+=1
print(ans)

Solution (Python):
import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mat = lambda x, y, v: [[v]*y for _ in range(x)]
ten = lambda x, y, z, v: [mat(y, z, v) for _ in range(x)]
mod = 1000000007
sys.setrecursionlimit(1000000)

N = ri()
C = rs()

l, r = 0, N-1
ans = 0
while l < r:
	while C[l] == 'R' and l < N-1:
		l += 1
	while C[r] == 'W' and r > 0:
		r -= 1
	if l >= r: break
	l += 1
	r -= 1
	ans += 1
print(ans)

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

r_n = 0
ans = 0
for c in C:
    if c == 'R':
        r_n += 1

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

print(ans)

Solution (Python):
N = int(input())
c = list(input())
Rn = c.count("R") 
Wn = 0 
ans = N
for i in range(N+1):
    if i == 0:
        ans = Rn
    else:
        if c[i-1] == "R":
            Rn -= 1
        else:
            Wn += 1
        ans = min(ans, min(Rn, Wn) + abs(Rn - Wn))
print(ans)

Solution (Python):
def solve(string):
    _, c = string.split()
    r = c.count("R")
    return str(c[:r].count("W"))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
def solve(string):
    n, c = string.split()
    n, c = int(n), list(c)
    wi, ri = 0, n - 1
    while wi < n and c[wi] == "R":
        wi += 1
    while ri >= 0 and c[ri] == "W":
        ri -= 1
    ans = 0
    while wi < n and ri >= 0 and wi < ri:
        c[wi], c[ri], ans = c[ri], c[wi], ans + 1
        wi, ri = wi + 1, ri - 1
        while wi < n and c[wi] == "R":
            wi += 1
        while ri >= 0 and c[ri] == "W":
            ri -= 1
    return str(ans)
 
 
if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
def solve(string):
    n, c = string.split()
    n, c = int(n), list(c)
    wi, ri = 0, n - 1
    while wi < n and c[wi] == "R":
        wi += 1
    while ri >= 0 and c[ri] == "W":
        ri -= 1
    ans = 0
    while wi < n and ri >= 0 and wi < ri:
        c[wi], c[ri], ans = c[ri], c[wi], ans + 1
        wi, ri = wi + 1, ri - 1
        while wi < n and c[wi] == "R":
            wi += 1
        while ri >= 0 and c[ri] == "W":
            ri -= 1
    return str(ans)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
n = int(input())
c = input()
p = []
for i in range(n):
  if c[i]=='R':
    p += [i]
print(len([1 for i in p if i>=len(p)]))

Solution (Python):
import math
N = int(input())
C = str(input())

C = C.replace('W', '1').replace('R', '0')
C = list(C)
sort_C = sorted(C)

cnt = 0

for i in range(N):
    if C[i] != sort_C[i]:
        cnt += 1

print(math.ceil(cnt/2))

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

import math

rkosu=c.count("R")
wkosu=n-rkosu

ca=["R"]*rkosu+["W"]*wkosu

count=0
for i in range(n):
    if c[i] != ca[i]:
        count+=1

ans=math.ceil(count/2)
print(ans)

Solution (Python):
n = int(input())
li_c = input()
r_index = [i for i,x in enumerate(li_c) if x=="R"]
w_index = [i for i,x in enumerate(li_c) if x=="W"]
r_num = len(r_index)
w_num = len(w_index)

R = r_num
W = 0
ans = max(R, W)
for i in range(n):
    if li_c[i]=="R":
        R -= 1
    else:
        W += 1
    ans = min(ans, max(R, W))
print(ans)

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

R_r = c.count("R")
W_l = 0
ans = N
for i in range(N+1):
  t = max(R_r,W_l)
  ans = min(ans, t)
  if R_r == 0:
    break
  if c[i] == "R":
    R_r -= 1
  else:
    W_l += 1

print(ans)

Solution (Python):
N = int(input())
c = list(input())
w = 0
r = N-1
ans = 0
while w < r:
    for i in range(w,r):
        if c[i] == "W":
            c[i] = "R"
            w = i
            break
    else:
        break
    for j in range(r,w,-1):
        if c[j] == "R":
            c[j] = "W"
            r = j
            break
    else:
        break
    ans += 1
print(ans)

Solution (Python):



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

from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect
import heapq














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


INF = float('inf')
from sys import stdin
readline = stdin.readline
def readInts():
  return list(map(int,readline().split()))
def readTuples():
    return tuple(map(int,readline().split()))
def I():
    return int(readline())
n = I()
s = input()
C = Counter(s)
w,r = 0,0
for k,v in C.items():
    if k == 'W':
        w += v
    else:
        r += v
p = 'R' * r + 'W' * w
ans = 0
for i in range(n):
    if p[i] == s[i]:
        pass
    else:
        ans += 1
print(ans//2)

Solution (Python):
number = int(input())
date = input()
date = list(date)
cnt =0
cnt2 =0

for i in range(number):
  if date[i] =="R":
    cnt +=1

for s in range(cnt):
  if date[s] =="W":
    cnt2 +=1
print(cnt2)

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

s = list(input())

count_red = s.count('R')
count_white = 0

A = []
for i in range(n):
    if s[i] == 'R':
        count_red -= 1
    if s[i] == 'W':
        count_white += 1
    A.append(max(count_red, count_white))

if len(set(s)) == 1 and list(set(s)) == ['W']:
    print(0)
else:
    print(min(A))

Solution (Python):
n = int(input())
c = input()
cnt = [0 for _ in range(n)]
red = 0
for i in range(n):
   if c[i] == 'R': red += 1
   cnt[i] = red

print(red - cnt[red-1])

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

W_total = C.count("W")
R_total = N - W_total
w_cur = 0
r_cur = R_total
ans = R_total
cur = 0

for i in range(N):
    if C[i] == "W":
        w_cur += 1
    else:
        r_cur -= 1

    ans = min(ans, min(w_cur, r_cur) + abs(w_cur - r_cur))

print(ans)

Solution (Python):


from bisect import bisect_left
import sys

N = int(input())
c =input().strip()
ans =0
r =[i for i, x in enumerate(c) if x == 'R'] 
r_num =len(r) 

ans =bisect_left(r,r_num) 
print(r_num -ans)

Solution (Python):

from bisect import bisect_left
import sys

N = int(input())
c =input().strip()
ans =0
w =[i for i, x in enumerate(c) if x == 'W']
r =[i for i, x in enumerate(c) if x == 'R']
w_num =len(w) 
r_num =len(r) 

ans =bisect_left(r,r_num)
print(r_num -ans)

Solution (Python):
N=int(input())
c=input()
R=c.count('R')
RR=c[0:R].count('R')
print(min(R-RR,R,N-R))

Solution (Python):
def main():
    n = int(input())
    c = str(input())

    w_count = 0
    for i in range(n):
        if c[i] == 'W':
            w_count += 1

    w_count_ano = 0
    for i in range(w_count):
        if c[- 1 - i] == 'W':
            w_count_ano += 1

    count = w_count - w_count_ano
    print(count)


if __name__ == '__main__':
    main()

Solution (Python):
class Alter:
    def __init__(self, N, C):
        self.N = N
        self.C = C

        whites = 0
        for c in C:
            if c == "W":
                whites += 1
        self.white_count = whites
        self.red_count = N - whites

    def solve(self):
        result = 0
        if self.white_count == self.red_count:
            for i in range(self.red_count):
                if self.C[i] == "W":
                    result += 1
            print(result)
        elif self.white_count > self.red_count:
            for i in range(self.red_count):
                if self.C[i] == "W":
                    result += 1
            print(result)
        else:
            for i in range(self.white_count):
                w_i = self.N - i - 1
                if self.C[w_i] == "R":
                    result += 1
            print(result)

N = int(input())

c_line = input()
C = [None] * N
for i, c in enumerate(c_line):
    C[i] = c

alter = Alter(N, C)
alter.solve()

Solution (Python):
_,C=input(),input()
a=C.count('R')
print(a-C[:a].count('R'))

Solution (Python):
N = int(input())
C = list(input())
count = list()
W = 0
R = 0

for i in C:
    if i == "R":
        R += 1

if R == 0:
    count.append(0)

for i in C:

    if i == "W":
        W += 1
    else:
        R -= 1

    if W <= R:
        count.append(R)
    else:
        count.append(W)

print(min(count))

Solution (Python):
import sys
sys.setrecursionlimit(300000)

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI0(): return map(lambda s: int(s) - 1, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
def LMI0(): return list(map(lambda s: int(s) - 1, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N = I()
C = input()

r_cnt = C.count('R')
ans = r_cnt - C[:r_cnt].count('R')
print(ans)

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

w = c.count("W")
r = c.count("R")

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

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

r_sum = 0
for i in range(N):
    if str[i] == "R":
        r_sum += 1
r_sum2 = 0
for i in range(r_sum):
    if str[i] == "R":
        r_sum2 += 1

print(r_sum - r_sum2)

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


w = c.count("W")
r = c.count("R")

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

Solution (Python):
N=int(input())
C=list(input())
rednum=C.count('R')
rednumleft=0
for i in range(rednum):
    if C[i]=='R':
        rednumleft+=1
print(rednum-rednumleft)

Solution (Python):
from collections import Counter
n = int(input())
c = list(input())
num = Counter(c)
r = num["R"]

ans = ["R"]*r + ["W"]*(n-r)
cnt = 0

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

Solution (Python):


import collections

def main():
    n = int(input())
    s = input()
    a_list = []
    for i in range(n):
        if s[i] == "W":
            a_list.append(0)
        else:
            a_list.append(1)
    r_count = sum(a_list)
    print(r_count - sum(a_list[0:r_count]))


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):
from sys import stdin
n = int(stdin.readline())
c = stdin.readline()[:-1]

red = c.count("R")
ans = red
numwhite = 0
for i in range(n):
    numwhite += 1 if c[i] == "W" else 0
    numred = i - numwhite + 1
    resred = red - numred
    ans = min(ans,max(resred,numwhite))
print(ans)

Solution (Python):
from sys import stdin
n = int(stdin.readline())
c = stdin.readline()[:-1]

red = c.count("R")
ans = red
numwhite = 0
for i in range(n):
    numwhite += 1 if c[i] == "W" else 0
    numred = i - numwhite + 1
    resred = red - numred
    if resred == numwhite:
        ans = min(ans,resred)
print(ans)

Solution (Python):
n = int(input())
c = input()
x = 0
for i in range(n):
    if c[i] == "R":
        x += 1

y = 0
if x != 0:
    for j in range(x):
        if c[j] == "W":
            y += 1

print(y)

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

a = 0
b = 0

for i in range(N):
    if s[i] == 'R':
        a = a + 1
ans = max(a,b)
for i in range(N):
    if s[i] == 'R':
        a = a - 1
    else:
        b = b + 1
    now = max(a,b)
    ans = min(ans, now)
print(ans)

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

    rc = C.count('R')
    rhc = C[:rc].count('R')
    print(rc - rhc)


if __name__ == '__main__':
    main()

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

sx = len(set(x))
if sx == 1:
    print(0)
    exit()

cnt_r = x.count("R")
cnt = 0
for i in range(cnt_r):
    if x[i] == "W":
        cnt += 1
print(cnt)

Solution (Python):
n = int(input())
wr = input()
R_cnt = wr.count('R')
ans = wr.count('W',0,R_cnt)
print(ans)

Solution (Python):
def main():
    n = int(input())
    s = input()
    rcount = s.count('R')
    diff = s[0:rcount].count("W")

    print(diff)




if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
s=input()
A=[]
for i in range(n):
  A.append(s[i])
W=0
R=A.count('R')
ans=float('inf')
for i in range(n+1):
  if i!=0:
    if A[i-1]=='W':
      W+=1
    else:
      R-=1
  ans=min(max(W,R),ans)
print(ans)

Solution (Python):
import math, sys

N=int(input())
S=input()
R_count=0
if "R" not in S:
    print(0)
    sys.exit()
if "W" not in S:
    print(0)
    sys.exit()
if "R" not in S[S.index("W"):]:
    print(0)
    sys.exit()
for i in range(N):
    if S[i]=="R":
        R_count+=1

S=S[:R_count]
ans=S.count("W")
print(ans)

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

cs = input()

w_count = 0
for c in cs:
    if c == 'W':
        w_count += 1

if w_count == 0:
    print(0)
    exit()

rest = cs[-w_count:]

answer = 0
for c in rest:
    if c == 'R':
        answer += 1
print(answer)

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

i = 0
j = N - 1
count = 0

while t:
	if c[i] == "R":
		i += 1
	elif c[j] == "W":
		j -= 1
	elif c[i] == "W" and c[j] == "R":
		c[i] = "R"
		c[j] = "W"
		count += 1	
	if i >= j:
		t = False
		break
print(count)

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

def resolve():
    N = map(int, input().split())
    input_str = input()
    r_cnt=0;
    for s in input_str:
        
        if s =='R':
            r_cnt+=1
    ans = 0
    for i in range(r_cnt):
        if input_str[i] == 'W':
            ans+=1
    print(ans)


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

    def test_入力例_1(self):
        input = """4
WWRR"""
        output = """2"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """2
RR"""
        output = """0"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """8
WRWWRWRR"""
        output = """3"""
        self.assertIO(input, output)


if __name__ == "__main__":
    resolve()

Solution (Python):
n=int(input())
C = list(input())
ans = 0
N = n-1
for l in range(n):
    if C[l] == "W":
        for r in range(N,0,-1):
            if r<=l:
                print(ans)
                exit()
            if C[r] == "R":
                
                C[l] = "R"
                C[r] = "W"
                
                
                ans += 1
                N = r-1
                break
print(ans)

Solution (Python):

from collections import Counter
n = int(input())
c = list(input())
d = Counter(c)


e=Counter(c[:d["R"]])

print(d["R"]-e["R"])

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

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

last_c='R'*r_cnt+'W'*w_cnt

num=0
for i in range(n):
  if c[i]!=last_c[i]:
    num+=1

print((num+2-1)//2)

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

r_cnt = A.count('R')

ans = 0
for i in range(r_cnt):
    if A[i] != 'R':
        ans += 1

print(ans)

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


count=Counter(C)
ws=count["W"]
rs=count["R"]

outws=0
outrs=0
for i in range(N):
    c=C[i]
    if c=="R":
        if not 0<=i<rs:
            outrs+=1
    else:
        if 0<=i<rs:
            outws+=1

ans=max(outrs, outws)
print(ans)

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

a, b = 0, 0
for i in range(n):
    if C[i] == "R":
        a += 1
    ans = max(a, b)

for i in range(n):
    if C[i] == "R":
        a -= 1
    else:
        b += 1
    tmp = max(a, b)
    ans = min(ans, tmp)

print(ans)

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

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

W = C.count("W")
R = C.count("R")
w = 0
r = R
ans = float('inf')

for c in C:
    if c == "W":
        w += 1
    else:
        r -= 1
    ans = min(ans, max(w, r))
print(ans)

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

r = s.count('R')

ans=0
for i in range(r):
    if s[i]=='W':
        ans+=1

print(ans)

Solution (Python):
N = int(input())
x = input().strip()
cnt = 0
for i in range(N):
    if x[i]=="R":
        cnt += 1
if cnt==0 or cnt==N:
    print(0)
else:
    ans = 0
    for i in range(cnt):
        if x[i]=="W":
            ans += 1
    print(ans)

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

r = S.count('R')
ans = r - S[:r].count('R')
print(ans)

Solution (Python):
import sys
from collections import Counter

input = sys.stdin.readline
N = int(input())
C = list(input().strip())

counter = Counter(C)
if len(counter) == 1:
    print(0)
else:
    red_n = counter["R"]
    ans = 0
    for i in range(N):
        if red_n <= i:
            break
        if C[i] == "W":
            ans += 1
    print(ans)

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

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

Solution (Python):
from collections import Counter


N = int(input())
c = input()
cntr = Counter(c)

ans = 0
for c, r in zip(c, "R" * cntr["R"]):
    if c != r:
        ans += 1
print(ans)

Solution (Python):

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


red_num = str.count(C, "R")


white_num_left = str.count(C[:red_num], "W")


min_ope = white_num_left


print(min_ope)

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

w = 0
r = c.count('R')
i = 0
ans = max(w, r)
while i <= n - 1:
    if c[i] == 'W':
        w += 1
    else:
        r -= 1
    ans = min(ans, max(w, r))
    i += 1
print(ans)

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

n=0
c=0
for i in range(N)[::-1]:
    if C[i]=="R":
        while n<i:
            if C[n]=="W":
                c+=1
                n+=1
                break
            n+=1
print(c)

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

count_red = 0
count_white = 0

for i in range(N):
  if Stones[i] == "R":
    count_red += 1
    
for i in range(count_red):
  if Stones[i] == "W":
    count_white += 1
    
print(count_white)

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

Rn = C.count('R')
print(C[:Rn].count('W'))

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

f = 0
b = N - 1
ans = 0

while True:
    while  f <= N -1 and C[f] == "R":
        f += 1
    while b >= 0 and C[b] == "W":
        b -= 1
    if f > b:
        print(ans)
        break
    ans += 1
    f += 1
    b -= 1

Solution (Python):
n = int(input())
c = list(input())
rcnt = 0
allrcnt = 0
for i in range(len(c)):
    if c[i]=='R':
        allrcnt += 1
for i in range(len(c)):
    if i+1<=allrcnt and c[i]=='R':
        rcnt += 1

ans = allrcnt-rcnt

print(ans)

Solution (Python):
n = int(input())
c = list(input())
hrcnt = 0
rcnt = 0
for i in range(len(c)):
    if c[i]=='R':
        rcnt += 1
for i in range(len(c)):
    if i+1<=rcnt and c[i]=='R':
        hrcnt += 1

wcnt = len(c)-rcnt
if rcnt==len(c) or rcnt==0:
    ans = 0
else:
    ans = rcnt-hrcnt

print(ans)

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

c = list(input())
cnt = 0
l = 0
r = n-1
while l < r:
    while c[l] == "R" and l < r:
        l += 1

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

    if c[l] == c[r]:
        break

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

    l += 1
    r -= 1
    cnt += 1

print(cnt)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
def main():
    n = int(input())
    C = input()
    C_cunt = Counter(C)
    cunt_r = C_cunt['R']
    ans = 0
    for i in range(cunt_r):
        if C[i] == 'W':
            ans += 1
    print(ans)

if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
def main():
    n = int(input())
    C = input()
    C_cunt = Counter(C)
    cunt_r = C_cunt['R']
    ans = 0
    for i in range(cunt_r):
        if C[i] == 'W':
            ans += 1
    print(ans)

if __name__=='__main__':
    main()

Solution (Python):
import math
n = int(input())
ar = input()
w = 0
r = 0
ans = 0
for i in ar:
  if i == 'W':
    w += 1
  else:
    r += 1
if w == 0 or r == 0:
  print(ans)
else:
  for j in range(-1,-len(ar)-1,-1):
    if w == 0:
      break
    if ar[j] == 'R':
      ans += 1
    w -= 1
  print(ans)

Solution (Python):
import sys
input = sys.stdin.readline




'''
a=[]
b=[]
for i in range():
    A, B = map(int, input().split())
    a.append(A)   
    b.append(B)'''

n=int(input())
c=list(input().rstrip("\n"))
import collections
cc=collections.Counter(c)
ans=0
for i in range(cc["R"]):
    if c[i]=="W":
        ans+=1
print(ans)

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



rr, rl = c.count("R"), 0


if rr == 0 or rr == N:
  print(0)
  exit()



wr, wl = N - rr, 0

for i in range(N + 1):
  if i == 0:
    ans = min(rr, wr)
    continue

  if c[i-1] == "R":
    rr -= 1
    rl += 1
  else:
    wr -= 1
    wl += 1

  if rr >= wl:
    ans = min(ans, rr)
  else: ans = min(ans, wl)

print(ans)