qyliang/CodeNanoFix huggingface dataset:
Buggy code:
n, k = map(int, input().split())
s = input()
pre = s[0]
rsans = s[0]
first = s[0]

if s[0] == "R":
    second = "L"
else:
    second = "R"

for i in s[1:]:
    if i == second and k > 0:
        ans = ans + first
        pre = i
    elif i == first and pre == second:
        k -= 1
        ans = ans + i
        pre = i
    elif k == 0:
        ans = ans + i
    elif i == first and pre == first:
        ans = ans + i

pre = "0"
cnt = 0

for i in ans:
    if i == pre:
        cnt += 1
    pre = i

print(cnt)

Fixed code:
n, k = map(int, input().split())
s = input()
pre = s[0]
ans = s[0]
first = s[0]

if s[0] == "R":
    second = "L"
else:
    second = "R"

for i in s[1:]:
    if i == second and k > 0:
        ans = ans + first
        pre = i
    elif i == first and pre == second:
        k -= 1
        ans = ans + i
        pre = i
    elif k == 0:
        ans = ans + i
    elif i == first and pre == first:
        ans = ans + i

pre = "0"
cnt = 0

for i in ans:
    if i == pre:
        cnt += 1
    pre = i

print(cnt)
Buggy code:
#!/usr/bin/env python
# coding: utf-8

def ri():
    return int(input())

def rl():
    return list(input().split())

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

def calc(s, k):
    ret = 0
    rev = 0
    c = s[0]
    for i in range(1, len(s)):
        if s[i] == c:
            ret += 1
            continue
        rev += 1
        c = s[i]
    ret += min(rev, k*2)
    k -= rev//2
    return ret


def main():
    n, k = rli()
    s = input()
    rs = ""
    for i in range(len(s)):
        if (s[n-i-1] == 'L'): rs += 'R'
        else: rs += 'L'
    a1 = calc(s,k)
    a2 = calc(rs,k)
    print(s)
    print(rs)
    print(max(a1, a2))


if __name__ == '__main__':
    main()


Fixed code:
#!/usr/bin/env python
# coding: utf-8

def ri():
    return int(input())

def rl():
    return list(input().split())

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

def calc(s, k):
    ret = 0
    rev = 0
    c = s[0]
    for i in range(1, len(s)):
        if s[i] == c:
            ret += 1
            continue
        rev += 1
        c = s[i]
    ret += min(rev, k*2)
    k -= rev//2
    return ret


def main():
    n, k = rli()
    s = input()
    rs = ""
    for i in range(len(s)):
        if (s[n-i-1] == 'L'): rs += 'R'
        else: rs += 'L'
    a1 = calc(s,k)
    a2 = calc(rs,k)
    # print(s)
    # print(rs)
    print(max(a1, a2))


if __name__ == '__main__':
    main()
Buggy code:
N, K = map(int, input().split())
S = input()

def g(t):
    l = []
    b = "x"
    c = 0
    for i, t_i in enumerate(t):
        if b != t_i:
            l.append(c)
            c = 0
        c += 1
        b = t_i
    l.append(c)
    return l[1:]

l = g(S)
len_l = len(l)
score = sum(l)-len_l
if len_l%2:
    score += min(len_l//2, K)*2
else:
    score += min(len_l//2, K)*2
    if len_l//2 <= K:
        print(score)
        score -= 1

print(score)

Fixed code:
N, K = map(int, input().split())
S = input()

def g(t):
    l = []
    b = "x"
    c = 0
    for i, t_i in enumerate(t):
        if b != t_i:
            l.append(c)
            c = 0
        c += 1
        b = t_i
    l.append(c)
    return l[1:]

l = g(S)
len_l = len(l)
score = sum(l)-len_l

score += min(len_l//2, K)*2 - (len_l%2==0 and len_l//2 <= K)

print(score)
Buggy code:
n,k=map(int,input().split())
s=list(input())
point=s[0]
count=0
while k!=0 and count!=n-1:
    if s[count]!=point:
        while True:
            if s[count]!=point:
                s[count]=point
                count +=1
                if count==n:break
            else:break
        k -=1
    else:count +=1

print("".join(s))
ans=0
for i in range(n-1):
    if s[i]==s[i+1]:
        ans +=1
print(ans)

Fixed code:
n,k=map(int,input().split())
s=list(input())
point=s[0]
count=0
while k!=0 and count!=n:
    if s[count]!=point:
        while True:
            if count==n:break
            elif s[count]!=point:
                s[count]=point
                count +=1
            else:break
        k -=1
    else:count +=1

#print("".join(s))
ans=0
for i in range(n-1):
    if s[i]==s[i+1]:
        ans +=1
print(ans)
Buggy code:
n,k=map(int,input().split())
s=list(input())
count=0
for i in range(n-1):
    if s[i]==s[i+1]:
        count +=1
print(count+k*2)

Fixed code:
n,k=map(int,input().split())
s=list(input())
count=0
for i in range(n-1):
    if s[i]==s[i+1]:
        count +=1
print(min(n-1,count+k*2))
Buggy code:
N,K = list(map(int,input().split()))
S = input()
lc = S.count('L')
rc = N-lc
maxc = lc+rc-2
nowc = 0
now = S[0]
for i in range(1,N):
    if S[i] == now:
        nowc += 1
    else:
        now = S[i]
print(min(nowc+2*K,maxc))

Fixed code:
N,K = list(map(int,input().split()))
S = input()
if N == 1:
    print(0)
else:
    lc = max(S.count('L')-1,0)
    rc = max(N-lc-1,0)
    maxc = lc+rc
    nowc = 0
    now = S[0]
    for i in range(1,N):
        if S[i] == now:
            nowc += 1
        else:
            now = S[i]
    print(min(nowc+2*K,maxc))
Buggy code:
n,k=map(int,input().split())
s=input()
t=list()
ans=0
count=1
for i in range(n-1):
	if s[i]==s[i+1]:
		count+=1
	else:
		if count >= 2:
			ans += count-1
		t.append(1)
		count=1
if count >= 2:
	ans += count-1
t.append(1)
if len(t) <= 2*k+1:
	ans += len(t)
else:
	ans += 2*k
print(ans)

Fixed code:
n,k=map(int,input().split())
s=input()
t=list()
ans=0
count=1
for i in range(n-1):
	if s[i]==s[i+1]:
		count+=1
	else:
		if count >= 2:
			ans += count-1
		t.append(1)
		count=1
if count >= 2:
	ans += count-1
t.append(1)
if len(t) < 2*k+1:
	ans += len(t) - 1
else:
	ans += 2*k
print(ans)
Buggy code:
# -*- coding utf-8 -*-

MOD = 10 ** 9 + 7

N, K = map(int, input().split())
S = 'R' + input() + 'L'

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

ans = min(ans + 2 * K, len(S) - 1)

print(ans)


Fixed code:
# -*- coding utf-8 -*-

MOD = 10 ** 9 + 7

N, K = map(int, input().split())
S = 'R' + input() + 'L'

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

ans = min(ans + 2 * K, N - 1)

print(ans)
Buggy code:
n, k = map(int, input().split())
s = str(input())

lr = 0
rl = 0
for i in range(1, n):
    if s[i-1] == "L" and s[i] == "R":
        lr += 1
    if s[i-1] == "R" and s[i] == "L":
        rl += 1
ans = n - (lr + rl + 1) + 2 * min(min(lr, rl), k)

if k < min(lr, rl):
    if lr > rl and (s[0] == "L" or s[-1] == "R"):
        ans += 1
    if lr < rl and (s[0] == "R" or s[-1] == "L"):
        ans += 1
print(ans)

Fixed code:
n, k = map(int, input().split())
s = str(input())

lr = 0
rl = 0
for i in range(1, n):
    if s[i-1] == "L" and s[i] == "R":
        lr += 1
    if s[i-1] == "R" and s[i] == "L":
        rl += 1
ans = n - (lr + rl + 1) + (2 * min(min(lr, rl), k))

if k > min(lr, rl):
    if lr > rl and (s[0] == "L" or s[-1] == "R"):
        ans += 1
    if lr < rl and (s[0] == "R" or s[-1] == "L"):
        ans += 1
print(ans)
Buggy code:
N,K=map(int,input().split())
S=input()

cnt = 0
bef = "M"
for i in range(0,len(S)):
    if i == len(S) - 1 and S[i] == "R":
        cnt = cnt
    elif S[i-1] == "L" and S[i] == "L":
        cnt += 1
    elif S[i] == "R" and S[i+1] == "R":
        cnt += 1
        
    bef = S[i]
    i += 1
print(min(cnt + 2 * K, N - 1))

Fixed code:
N,K=map(int,input().split())
S=input()

cnt = 0
for i in range(0,len(S)):
    if i == len(S) - 1 and S[i] == "R":
        cnt = cnt
    elif i > 0 and  S[i-1] == "L" and S[i] == "L":
        cnt += 1
    elif S[i] == "R" and S[i+1] == "R":
        cnt += 1
    i += 1
print(min(cnt + 2 * K, N - 1))
Buggy code:
n,k = map(int,input().split())
S = input()

C = []

count = 0
dotti = S[0]
ans = 0

for i in range(n):
    if dotti != S[i]:
        count += 1
    if S[i] == "R" and i < n-1: 
        if S[i+1] == "R":
            ans += 1
    elif S[i] == "L" and i > 0: 
        if S[i-1] == "L":
            ans += 1

while k > 0:
    if count > 2:
        ans += 2
        count -= 1
    elif count == 2:
        ans += 1
        break
    k -= 1

print(ans)

Fixed code:
n,k = map(int,input().split())
S = input()

C = []

count = 0
dotti = S[0]
ans = 0

for i in range(n):
    if dotti != S[i]:
        count += 1
        dotti = S[i]
    if S[i] == "R" and i < n-1: 
        if S[i+1] == "R":
            ans += 1
    elif S[i] == "L" and i > 0: 
        if S[i-1] == "L":
            ans += 1

while k > 0:
    if count >= 2:
        ans += 2
        count -= 2
    elif count == 1:
        ans += 1
        count -= 1
        break
    else:
        break
    k -= 1

print(ans)
Buggy code:
import sys

#+++++
		
def main():
	n, k = map(int, input().split())
	sl = input()
	sls='R'+sl+'L'
	ss=0
	for cp, cn in zip(sls[:-1], sls[1:]):
		#print(cp,cn)
		if cp == cn and cp == 'R':
			ss+=1
			
	for cp, cn in zip(sls[1:], sls[2:]):
		#print(cp,cn)
		if cp == cn and cp == 'L':
			ss+=1	
			
	nh=n-ss
	#print(nh,k,ss)
	ss += min(nh-1,k*2)
	
	
	print(ss)
	
	
#+++++


if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Fixed code:
import sys

#+++++
		
def main():
	n, k = map(int, input().split())
	sl = input()
	ss=0
	for cp, cn in zip(sl, sl[1:]):
		#print(cp,cn)
		if cp == cn and cp == 'L':
			ss+=1
			
	for cp, cn in zip(sl, sl[1:]):
		#print(cp,cn)
		if cp == cn and cp == 'R':
			ss+=1	
			
	nh=n-ss
	#print(nh,k,ss)
	ss += min(nh-1,k*2)
	
	
	print(ss)
	
	
#+++++


if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)
Buggy code:
import sys
sys.setrecursionlimit(10**7)
N, K = map(int, input().split())
S = input()

cnt1 = 0
cnt2 = 0

for i in range(N-1):
    # edge
    if i == 0 and S[i] == 'L':
        cnt1 += 1
    if i == N-2 and S[i+1] == 'R':
        cnt1 += 1 

    # not edge
    if S[i] == S[i+1]:
        continue
    elif S[i] == 'L' and S[i+1] == 'R':
        continue
    elif S[i] == 'R' and S[i+1] == 'L':
        cnt2 += 1

print('cnt1:', cnt1)
print('cnt2:', cnt2)

if cnt2 >= K:
    print(max(1, N - (cnt2-K)*2 - cnt1))
else:
    print(max(1, N - max(0, (cnt1 - (K - cnt2)))))


Fixed code:
import sys
sys.setrecursionlimit(10**7)
N, K = map(int, input().split())
S = input()

cnt1 = 0
cnt2 = 0

for i in range(N-1):
    # edge
    if i == 0 and S[i] == 'L':
        cnt1 += 1
    if i == N-2 and S[i+1] == 'R':
        cnt1 += 1 

    # not edge
    if S[i] == S[i+1]:
        continue
    elif S[i] == 'L' and S[i+1] == 'R':
        continue
    elif S[i] == 'R' and S[i+1] == 'L':
        cnt2 += 1

if cnt2 >= K:
    print(min(N-1, N - (cnt2-K)*2 - cnt1))
else:
    print(min(N-1, N - max(0, (cnt1 - (K - cnt2)))))
Buggy code:
import re
n, k = [int(i) for i in input().split()]
s = input()
cnt = sum([1 for i in range(n-1) if s[i+1] == s[i]])
print(cnt)
flg = s[-1] == "R"
s += "L"
red_ls = re.findall(r"RL", s)
if len(red_ls) > k:
    print(cnt + k * 2)
else:
    if flg:
        print(cnt + (len(red_ls) - 1) * 2 + 1)
    else:
        print(cnt + len(red_ls) * 2)

Fixed code:
import re
n, k = [int(i) for i in input().split()]
s = input()
cnt = sum([1 for i in range(n-1) if s[i+1] == s[i]])
flg = (s[-1] == "R") + (s[0] == "R")
s += "L"
red_ls = re.findall(r"RL", s)
if len(red_ls) > k:
    print(cnt + k * 2)
else:
    if flg > 0:
        print(cnt + (len(red_ls) - flg) * 2 + flg)
    else:
        print(cnt + len(red_ls) * 2)
Buggy code:
N,K = map(int,input().split())
S = input()

for i in range(len(S)-1):
    if S[i] == S[i+1]:
        count += 1

print(min(N-1,count+2*K))

Fixed code:
N ,K = map(int,input().split())
S = input()
count = 0

for i in range(N-1):
    if S[i] == S[i+1]:
        count += 1
print(min(N-1,count+2*K))
Buggy code:
N,K=list(map(int,input().split()))
S=input()
cnt=0

for i in range(N-1):
    if S[i]==S[i+1]:
        cnt+=1
print(cnt+K*2)

Fixed code:
N,K = list(map(int,input().split()))
S = input()
count=0
for i in range(N-1):
    if S[i]==S[i+1]:
        count+=1

print(min(N-1,count+2*K))
Buggy code:
N, K = map(int,input().split())
S = input()
group_size = 0
count = 0
for i in range(0,N):
  if i>0 and S[i] == S[i-1]:
    count += 1
  elif i >0 and S[i] != S[i-1]:
    group_size += 1

for i in range(K):
  if group_size > 2:
    count +=2
    group_size -=2
  elif group_size == 2:
    count += 1
    group_size -=1
  else:
    break
print(count)

  



Fixed code:
N, K = map(int,input().split())
S = input()
group_size = 1
count = 0
for i in range(0,N):
  if i>0 and S[i] == S[i-1]:
    count += 1
  elif i >0 and S[i] != S[i-1]:
    group_size += 1
for i in range(K):
  if group_size > 2:
    count +=2
    group_size -=2
  elif group_size == 2:
    count += 1
    group_size -=1
  else:
    break
print(count)
Buggy code:
n , k = [ (int) i for i in input().split()]
s =input()
a =0
for i , ch in enumerate(s[1:] , 1):
  if ch == s[i-1]:
   	a+=1
print( min( n -1, a +2*k)


Fixed code:
n , k = [ int(i) for i in input().split()]
s =input()
a =0
for i , ch in enumerate(s[1:] , 1):
  if ch == s[i-1]:
   	a+=1
print( min( n -1, a +2*k))
Buggy code:
a,b=input().strip().split(' ')
a,b=[int(a),int(b)]
c=input().strip()
d=0
if c[0]=='R':
  if c[1]=='R':
    d+=1
  
for i in range(1,a-1):
  if c[i]=='L':
    if c[i-1]=='L':
      d+=1
  elif c[i]=='R':
    if c[i+1]=='R':
      d+=1
if c[a-1]=='L':
  if c[a-2]=='L':
    d+=1
d=d+2*k
if d>(a-1):
  print (a-1)
else:
  print(d)
  
  

Fixed code:
a,b=input().strip().split(' ')
a,b=[int(a),int(b)]
c=input().strip()
d=0
if c[0]=='R':
  if c[1]=='R':
    d+=1
  
for i in range(1,a-1):
  if c[i]=='L':
    if c[i-1]=='L':
      d+=1
  elif c[i]=='R':
    if c[i+1]=='R':
      d+=1
if c[a-1]=='L':
  if c[a-2]=='L':
    d+=1
d=d+2*b
if d>(a-1):
  print (a-1)
else:
  print(d)
Buggy code:
from itertools import groupby
N,K = list(map(int,input().split()))
S = input()
A = []

for i,j in groupby(S):
  A.append(len(list(j))-1)

print(min(N,sum(A)+2*K))

Fixed code:
from itertools import groupby
N,K = list(map(int,input().split()))
S = input()
A = []

for i,j in groupby(S):
  A.append(len(list(j))-1)

print(min(N-1,sum(A)+2*K))
Buggy code:
N, K = map(int, input().split())
S = list(input())
r_cnt = 0
answer = 0
before = S[0]
for s in S:
  if s == before:
    answer += 1
  else:
    r_cnt += 1
    before = s
answer -= 1
print(answer, r_cnt)
if r_cnt%2 == 0:
  print(answer+min(r_cnt//2, K)*2)
elif r_cnt//2 >= K:
  print(answer+K*2)
else:
  print(answer+r_cnt*2-1)

Fixed code:
N, K = map(int, input().split())
S = list(input())
r_cnt = 0
answer = 0
before = S[0]
for s in S:
  if s == before:
    answer += 1
  else:
    r_cnt += 1
    before = s
answer -= 1
if r_cnt%2 == 0:
  print(answer+min(r_cnt//2, K)*2)
elif r_cnt//2 >= K:
  print(answer+K*2)
else:
  print(answer+(r_cnt//2+1)*2-1)
Buggy code:
n,k=map(int,input().split())
s=input()
li=[]
seq=1
for i in range(1,len(s)):
    if s[i-1]==s[i]:
        seq+=1
    else:
        li.append(seq)
        seq=1
        
li.append(seq)
length=len(li)
ans=sum(li)-length

for i in range(k):
    if length>=3:
        length+=-1
        ans=+2
    elif length==2:
        ans+=1
        length+=-1
    else:
        break


print(ans)


Fixed code:
n,k=map(int,input().split())
s=input()
li=[]
seq=1
for i in range(1,len(s)):
    if s[i-1]==s[i]:
        seq+=1
    else:
        li.append(seq)
        seq=1
        
li.append(seq)
length=len(li)
ans=sum(li)-length

for i in range(k):
    if length>=3:
        length+=-2
        ans+=2
    elif length==2:
        ans+=1
        length+=-1
    else:
        break


print(ans)
Buggy code:
#Face Produces Unhappiness

N, K = map(int, input().split())
S = input()

score = 0
for i in range(N):
    score += 1 if S[i-1] == S[i] else 0
ans = min(score+2*K, N-1)
print (ans)


Fixed code:
#Face Produces Unhappiness

N, K = map(int, input().split())
S = input()

score = 0
for i in range(N-1):
    if S[i] == S[i+1]: score += 1 
ans = min(score+2*K, N-1)
print (ans)
Buggy code:
import re

N, K = map(int, input().split())
S = input().strip()

LL = re.finditer('L+', S)
RR = re.finditer('R+', S)

happy = 0
next_happy = []

for l in LL:
    happy += len(l.group(0)) - 1
    if l.regs[0][0] == 0 or l.regs[0][1] == N:
        next_happy.append(1)
    else:
        next_happy.append(2)

for r in RR:
    happy += len(r.group(0)) - 1
    if r.regs[0][0] == 0 or r.regs[0][1] == N:
        next_happy.append(1)
    else:
        next_happy.append(2)

for i in range(K):
    happy += sum(sorted(next_happy, reverse=True)[:K])

if happy >= N:
    happy = N - 1

print(happy)


Fixed code:
import re

N, K = map(int, input().split())
S = input().strip()

LL = re.finditer('L+', S)
RR = re.finditer('R+', S)

happy = 0
next_happy = []

for l in LL:
    happy += len(l.group(0)) - 1
    if l.regs[0][0] == 0 or l.regs[0][1] == N:
        next_happy.append(1)
    else:
        next_happy.append(2)

for r in RR:
    happy += len(r.group(0)) - 1
    if r.regs[0][0] == 0 or r.regs[0][1] == N:
        next_happy.append(1)
    else:
        next_happy.append(2)

happy += sum(sorted(next_happy, reverse=True)[:K])

if happy >= N:
    happy = N - 1

print(happy)
Buggy code:
n,k=map(int,input().split())
s=input()
g=0
for i in range(n):
    if i!=0:
        if s[i-1]==s[i] and s[i]=="L":
            g+=1
    if i!=n-1:
        if s[i+1]==s[i] and s[i]=="R":
            g+=1
print(min(n-1,g+2*k) if (n-g-1)%2!=1 else min(n-2,g+2*k))

Fixed code:
n,k=map(int,input().split())
s=input()
g=0
for i in range(n):
    if i!=0:
        if s[i-1]==s[i] and s[i]=="L":
            g+=1
    if i!=n-1:
        if s[i+1]==s[i] and s[i]=="R":
            g+=1
print(min(n-1,g+2*k))
Buggy code:
n,k=map(int,input().split())
S=input()
c=0
d=0
for i in range(n):
    if S[i]==S[i+1]:
        c+=1
    if S[i]!=S[i+1]:
        d+=1
print(c+2*k-1 if d<=k and S[0]==S[-1] else c+2*k)

Fixed code:
n,k=map(int,input().split())
S=input()
c=0
d=0
for i in range(n-1):
    if S[i]==S[i+1]:
        c+=1
    if S[i]!=S[i+1]:
        d+=1
d=(d+1)//2
print(c+2*d-1 if (d<=k and S[0]!=S[-1]) else c+2*min(d,k))
Buggy code:
N,K = map(int,input().split())
P = list(input())
count = 0
count2 = 0
for i in range(N-1):
    if P[i] == P[i+1]:
        count2 += 1
if P[0] == 'L':
    for i in range(N - 1):
        if P[i] == 'L' and P[i + 1] == 'R':
            count += 1
else:
    for i in range(N - 1):
        if P[i] == 'R' and P[i + 1] == 'L':
            count += 1
if K > count:
    print(N-1)
elif K == count :
    print(count2 + 2 * (K-1))
else:
    print(count2 + 2 * K )




Fixed code:
N,K = map(int,input().split())
P = list(input())
count = 0
count2 = 0
for i in range(N-1):
    if P[i] == P[i+1]:
        count2 += 1
if P[0] == 'L':
    for i in range(N - 1):
        if P[i] == 'L' and P[i + 1] == 'R':
            count += 1
    if P[N-1] == 'L':
        print(count2 + 2*min(K,count))
    else:
        if K == count:
            print(count2 + (2*K) -1)
        elif K > count:
            print(count2 + (2 * count -1))
        else:
            print(count2 + (2*K))
else:
    for i in range(N - 1):
        if P[i] == 'R' and P[i + 1] == 'L':
            count += 1
    if P[N-1] == 'R':
        print(count2 + 2*min(K,count))
    else:
        if K == count:
            print(count2 + 2*K -1)
        elif K > count:
            print(count2 + 2*count -1)
        else:
            print(count2 + 2*K)
Buggy code:
N, K = [int(v) for v in input().split(' ')]
S = input()

A = []
for s in S:
    if s == 'L':
        A.append(0)
    else:
        A.append(1)

def count(S):
    prev_s = 1
    s = 1
    next_s = A[0]

    c = 0
    i = 1
    while i <= N:
        prev_s = s
        s = next_s
        if i < N:
            next_s = A[i]
        else:
            next_s = 0

        if s == 0: # L
            if s != prev_s:
                c += 1
        else: # R
            if s != next_s:
                c += 1
        i += 1
    return c

c = count(S)
# print(c)
print(N - (c - K * 2))

Fixed code:
N, K = [int(v) for v in input().split(' ')]
S = input()

A = []
for s in S:
    if s == 'L':
        A.append(0)
    else:
        A.append(1)

def count(S):

    s = -1
    next_s = A[0]

    c = 0
    i = 1
    while i <= N:
        prev_s = s
        s = next_s
        if i < N:
            next_s = A[i]
        else:
            next_s = -1

        if s == 0: # L
            if s != prev_s:
                c += 1
        else: # R
            if s != next_s:
                c += 1
        i += 1
    return c

unhappy = count(S)
unhappy -= K * 2
if unhappy <= 0:
    unhappy = 1
print(N - unhappy)
Buggy code:
N, K = map(int, input().split())
S = input()
h = 0
for i in range(N-1):
  if S[i] == S[i+1]:
    h += 1
ans = h + 2 * K
if ans > N:
  ans = N - 1
print(ans)

Fixed code:
N, K = map(int, input().split())
S = input()
h = 0
for i in range(N-1):
  if S[i] == S[i+1]:
    h += 1
ans = h + 2 * K
if ans >= N:
  ans = N - 1
print(ans)
Buggy code:
n, k = map(int, input().split())
s = input()

happy_count = 0
pre = ''
for index in range(n - 1):
  if index == 0:
    continue
  if s[index - 1] == s[index]:
    happy_count += 1

print(min(happy_count + (2 * k), n-1))


Fixed code:
n, k = map(int, input().split())
s = input()

happy_count = 0
pre = ''
for index in range(n - 1):
  if s[index + 1] == s[index]:
    happy_count += 1

print(min(happy_count + (2 * k), n-1))
Buggy code:
n,k  = map(int,input().split())
s = input()
res = 0
for i in range(1,n):
    if s[i] == s[i-1]:
        res += 1

print (res+k*2)

Fixed code:
n,k  = map(int,input().split())
s = input()
res = 0
for i in range(1,n):
    if s[i] == s[i-1]:
        res += 1

print (min(res+k*2,n-1))
Buggy code:
N,K=map(int, input().split())
S=input()
comp=[]
tmp=""

for i in range(N):
  if tmp=="":
    tmp=S[i]
    cnt=1
  else:
    if S[i]==tmp:
      cnt +=1
      
  if S[i]!=tmp:
    comp.append([tmp,cnt])
    tmp=S[i]
    cnt=1
  if i==N-1:
    comp.append([tmp,cnt])
  
ans=0  
for run in comp:
  ans += run[1]-1
ans += K*2
ans = min(ans,N)
print(ans)

Fixed code:
N,K=map(int, input().split())
S=input()
comp=[]
tmp=""

for i in range(N):
  if tmp=="":
    tmp=S[i]
    cnt=1
  else:
    if S[i]==tmp:
      cnt +=1
      
  if S[i]!=tmp:
    comp.append([tmp,cnt])
    tmp=S[i]
    cnt=1
  if i==N-1:
    comp.append([tmp,cnt])
  
ans=0  
for run in comp:
  ans += run[1]-1
ans += K*2
ans = min(ans,N-1)
print(ans)
Buggy code:
n,k = map(int,input().split())
s = list(input())
count = 0
for i in range(n):
    if i == 0:
        if s[i] == "R":
            if i < n-1:
                if s[i+1] == "R":
                    count += 1
    elif i == n-1:
        if s[i] == "L":
            if i > 0:
                if s[i-1] == "L":
                    count += 1
    else:
        if s[i] == "L":
            if s[i-1] == "L":
                count += 1
        else:
            if s[i+1] == "R":
                count += 1
m = n - count
if 2 * k >= m:
    print(n)
else:
    print(count + k*2)

Fixed code:
n,k = map(int,input().split())
s = list(input())
count = 0
for i in range(n):
    if i == 0:
        if s[i] == "R":
            if i < n-1:
                if s[i+1] == "R":
                    count += 1
    elif i == n-1:
        if s[i] == "L":
            if i > 0:
                if s[i-1] == "L":
                    count += 1
    else:
        if s[i] == "L":
            if s[i-1] == "L":
                count += 1
        else:
            if s[i+1] == "R":
                count += 1
print(min(n-1,count+2*k))
Buggy code:
n, k = map(int, input().split())
s = input()

i = 0
c = 0
cc = 0
while i < n:
	if s[i] == s[i - 1]:
		c += 1
	else:
		cc += 1

	i += 1

print(c + min(2 * k, cc))

Fixed code:
n, k = map(int, input().split())
s = input()

i = 1
c = 0
cc = 0
while i < n:
	if s[i] == s[i - 1]:
		c += 1
	else:
		cc += 1

	i += 1

print(c + min(2 * k, cc))