qyliang/CodeNanoFix huggingface dataset:
Buggy code:

def count(s):
	c=0
	
	for i in range(len(s)-1):
		if s[i+1] == s[i]:
			c += 1
	return c
	
n, k = map(int, input().split())
s = input()
a = [str(c) for c in s]
cmax = n-1
p = count(a)
while k>0:
	if p == cmax:
		break
	if p == cmax +1:
		break
	if p <=cmax:
		p += 2
		k -= 1
		
print(p)

Fixed code:
def count(s):
	c=0
	
	for i in range(len(s)-1):
		if s[i+1] == s[i]:
			c += 1
	return c
	
n, k = map(int, input().split())
s = input()
a = [str(c) for c in s]
cmax = n-1
p = count(a)
while 1:
  if p == cmax:
    break
  elif p == cmax +1:
    p -=1
    break
  elif k > 0:
    p += 2
    k -= 1
  else:
    break
		
print(p)
Buggy code:
n, k = map(int, input().split())
s = input()
cnt = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        cnt += 1
ans = cnt+2*k
print(ans)


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

n, k = map(int, input().split())
str = input()

pattern = re.compile("RL")
X = len(pattern.findall(str))
result = n - X * 2

if str[0] == 'L' and str[-1] == 'R':
    result -= 2
    Y = 1
elif str[0] == 'L' or str[-1] == 'R':
    result -= 1
    Y = 0

if X + Y <= k:
    result += 2 * X + Y
else:
    if X >= k:
        result += 2 * k
    else:
        result += 2 * X + (k-X)

print(result)





Fixed code:
import re

n, k = map(int, input().split())
str = input()

result = n-1

pattern = re.compile("RL")
X = len(pattern.findall(str))

if (str[0] == 'R') ^ (str[-1] == 'L'):
    Y = 0
else:
    if (str[0] == 'R') and (str[-1] == 'L'):
         X -= 1
    Y = 1

if k < X+Y:
    result -= Y + 2 * (X-k)

print(result)
Buggy code:


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



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

p = 0
while K > 0 and p < len(S):
	now = S[p]
	p += 1
	if p >= len(S):
		break
	while now == S[p]:
		p += 1
		if p >= len(S):
			break

	q = p + 1
	if q >= len(S):
		"""
		if S[p] == "R":
			S[p] = "L"
		else:
			S[p] = "R"
		"""
		break
	while S[p] == S[q]:
		q += 1
		if q >= len(S):
			break
	while p < q:
		if S[p] == "R":
			S[p] = "L"
		else:
			S[p] = "R"
		p += 1
		if p >= len(S):
			break
	K -= 1

ret = 0;
print(len(S))
for x in range(len(S)-1):
	print(x)
	if S[x] == S[x + 1]:
		ret += 1
print(ret)


Fixed code:
N, K = map(int, input().split())
S = list(input())
 
p = 0
while K > 0 and p < len(S):
	now = S[p]
	p += 1
	if p >= len(S):
		break
	while now == S[p]:
		p += 1
		if p >= len(S):
			break
			
	if p >= len(S):
		break
	q = p + 1
	if q >= len(S):
		if S[p] == "R":
			S[p] = "L"
		else:
			S[p] = "R"
		break
	while S[p] == S[q]:
		q += 1
		if q >= len(S):
			break
	while p < q:
		if S[p] == "R":
			S[p] = "L"
		else:
			S[p] = "R"
		p += 1
		if p >= len(S):
			break
	K -= 1
 
ret = 0;
for x in range(len(S)-1):
	if S[x] == S[x + 1]:
		ret += 1
print(ret)
Buggy code:
N, M = map(int, input().split())
S = list(input())
temp = S[0]
ind = 0
cs = [1]
for i in range(N - 1):
  if S[i] == S[i + 1]:
    cs[ind] += 1
  else:
    cs.append(1)
    ind += 1
    temp = S[i + 1]
#print(cs)
cscs = [cs[0]] + [0] * (len(cs))
for i in range(len(cs) - 1):
  cscs[i + 1] = cscs[i] + cs[i + 1]
cscs = [0] + cscs
css = [cs[0]] + [0] * (len(cs))
for i in range(len(cs) - 1):
  css[i + 1] = css[i] + cs[i + 1] - 1
css = [0] + css
#print(css)
#print(cscs)
res = 0
for i in range(len(cs) - 2 * M):
  temp = css[-2] - css[i + 2 * M] + css[max(i - 1, 0)]
  #print(temp,css[-2],css[i + 2 * M],css[max(i - 1, 0)])
  res = max(res, cscs[i + 2 * M] - cscs[i] + temp)
  #print(res - 1, i, i + 2 * M)
if len(css) - 2 <= 2 * M + 1:
  print(N - 1)
else:
  print(res - 1)



Fixed code:
N, M = map(int, input().split())
S = list(input())
temp = S[0]
ind = 0
cs = [1]
for i in range(N - 1):
  if S[i] == S[i + 1]:
    cs[ind] += 1
  else:
    cs.append(1)
    ind += 1
    temp = S[i + 1]
#print(cs)
cscs = [cs[0]] + [0] * (len(cs))
for i in range(len(cs) - 1):
  cscs[i + 1] = cscs[i] + cs[i + 1]
cscs = [0] + cscs
css = [cs[0]] + [0] * (len(cs))
for i in range(len(cs) - 1):
  css[i + 1] = css[i] + cs[i + 1] - 1
css = [0] + css
#print(css)
#print(cscs)
res = 0
for i in range(len(css) - 2 * M):
  temp = css[-2] - css[i + 2 * M] + css[max(i, 0)]
  #print(temp,css[-2],css[i + 2 * M],css[max(i - 1, 0)])
  res = max(res, cscs[i + 2 * M] - cscs[i] + temp)
  #print(res - 1, i, i + 2 * M)
if len(css) - 2 <= 2 * M + 1:
  print(N - 1)
else:
  print(res - 1)
Buggy code:
N, K = map(int, input().split())
S = input()

out = 0
prev_letter = ''
for letter in S:
    if letter == prev_letter:
        out += 1
    prev_letter = letter

change = N - out - 1
print('change', change)
print('out', out)

if change >= 2 * K:
    out += 2*K
else:
    out = N - 1
    
print(out)

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

out = 0
prev_letter = ''
for letter in S:
    if letter == prev_letter:
        out += 1
    prev_letter = letter

change = N - out - 1

if change >= 2 * K:
    out += 2*K
else:
    out = N - 1
    
print(out)
Buggy code:
(str_len,cng_cnt) = input().split(" ")
str_input = list(input())


print(str_len)
print(cng_cnt)
cng_cnt= int(cng_cnt)
#print(asd)
tmp1=""
cnt=0
cnt2=0
for st in str_input:
    if(tmp1 == st ):
      cnt = cnt+1
    else:
      cnt2= cnt2 + 1
      
    tmp1=st

if (cng_cnt < cnt2 ):
	c_up = cng_cnt * 2 
else:
	c_up= (cnt2 * 2) + (cng_cnt - cnt2)
ans =cnt + c_up

print(ans)

Fixed code:
(str_len,cng_cnt) = input().split(" ")
str_input = list(input())

str_len= int(str_len)
cng_cnt= int(cng_cnt)
tmp1=""
cnt=0
cnt2=0
for st in str_input:
    if(tmp1 == st ):
      cnt = cnt+1
    else:
      cnt2= cnt2 + 1
      
    tmp1=st

if (cng_cnt < cnt2-1 ):
	c_up = cng_cnt * 2 
else:
	c_up= (cnt2 * 2) + (cng_cnt - cnt2)
ans =cnt + c_up

if(ans >= str_len):
  ans =str_len-1

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

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

    for i in range(1, N-1):
        if S[i] == S[i+1]:
            happiness += 1

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


if __name__ == '__main__':
    d()


Fixed code:
def d():
    N, K = map(int,input().split())
    S = list(input())
    happiness = 0

    for i in range(N-1):
        if S[i] == S[i+1]:
            happiness += 1

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


if __name__ == '__main__':
    d()
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
count += k*2
if count > n:
    print(n)
else:
    print(count)

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
count += k*2
if count >= n:
    print(n-1)
else:
    print(count)
Buggy code:
N,K = list(map(int,input().split()))
S = input()
ans = 0
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        ans += 1
ans2 = ans + 2*K
print(ans2)

Fixed code:
N,K = list(map(int,input().split()))
S = input()
ans = 0
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        ans += 1
ans2 = min(ans + 2*K,N-1)
print(ans2)
Buggy code:
n,s=open(0);n,k=map(int,n.split());print(n-max(sum(i==j for i,j in zip(s,s[1:]))-2*k,1))

Fixed code:
n,s=open(0);n,k=map(int,n.split());print(n-max(sum(i!=j for i,j in zip(s,s[1:]))-2*k,1))
Buggy code:
N,K = map(int,input().split())
S = list(input())
a = S[0]
cur = 0
cnt = 0
for i in range(N):
    if S[i] != a:
        cnt += i-cur-1
        a = S[i]
        cur = i
print(min(cnt+2*K,N))

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

cnt = 0

i = 1
while i < len(s):
    print(i, s, cnt)
    if cnt == k:
        break

    if s[i] != s[i-1]:
        cnt += 1
        start = i
        target = s[i]

        while  i < len(s) and target == s[i]:
            if s[i] == 'R': s[i] = 'L'
            else: s[i] = 'R'

            i += 1   

        continue    

    i += 1

ans = 0
for i in range(1, len(s)):
    if s[i] == s[i-1]:
        ans += 1

print(ans)



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

cnt = 0

i = 1
while i < len(s):
    if cnt == k:
        break

    if s[i] != s[i-1]:
        cnt += 1
        start = i
        target = s[i]

        while  i < len(s) and target == s[i]:
            if s[i] == 'R': s[i] = 'L'
            else: s[i] = 'R'

            i += 1   

        continue    

    i += 1

ans = 0
for i in range(1, len(s)):
    if s[i] == s[i-1]:
        ans += 1

print(ans)
Buggy code:
N,M=map(int,input().split( ))

B = input()
C = []
for i in range(N-1):
    if B[i]!=B[i+1]:
        C.append(B[i])
C.append(B[N-1])

if len(C) % 2 == 1:
    print(min(M,len(C))*2+N-len(C))
else:
    if M<len(C)//2:
        print(M*2+N-len(C))
    else:
        print(len(C)*2+N-len(C)-1)


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

B = input()
C = []
for i in range(N-1):
    if B[i]!=B[i+1]:
        C.append(B[i])
C.append(B[N-1])

if len(C) % 2 == 1:
    print(min(M,len(C)//2)*2+N-len(C))
else:
    if M<len(C)//2:
        print(M*2+N-len(C))
    else:
        print(len(C)+N-len(C)-1)
Buggy code:
N, K = map(int, input().split())
S = input()
count = 0
for i in range(1, N):
  if S[i] == S[i - 1]:
    c += 1
print(min(N - 1, count + K * 2))

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

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

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

print(min(count+2*K, N-1))
Buggy code:
n, k = map(int, input().split())
s = int(input())

cnt = 0
for i in range(n):
    if s[i] != s[i+1]:
        cnt += 1
ans = min(count+2*k, n-1)


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

count = 0
ans = 0

for i in range(n-1):
    if s[i] != s[i+1]:
        count += 1
    else:
        ans += 1

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


Fixed code:
N, K = map(int, input().split())
S = input()
c = 0
for i in range(1, N):
  if S[i] == S[i - 1]:
    c += 1
print(min(N - 1, c + K * 2))
Buggy code:
N, K = [int(_c) for _c in input().split(" ")]
S = input()
point_list = []
for target, non_target in zip(["L", "R"], ["R", "L"]):
    S = list(S)

    start_idx_list = []
    before_char = None
    for i, c in enumerate(S):
        if i == 0:
            start_idx_list.append(i)
            before_char = c
        else:
            if before_char == non_target and c == target:
                start_idx_list.append(i)
            before_char = c

    longest = 0
    start_idx = -1
    end_idx = -1
    for i in range(len(start_idx_list) - K):
        cur_length = start_idx_list[i + K] - start_idx_list[i] + 1
        if cur_length > longest:
            start_idx = start_idx_list[i]
            end_idx = start_idx_list[i + K]
            longest = cur_length

    for _idx in range(start_idx, end_idx):
        S[_idx] = target
    S = ["N"] + S + ["N"]

    point = 0
    for idx in range(1, N + 1):
        if S[idx] == "L":
            if S[idx + 1] == "L":
                point += 1
        if S[idx] == "R":
            if S[idx - 1] == "R":
                point += 1

    point_list.append(point)

print(max(point_list))

Fixed code:
N, K = [int(_c) for _c in input().split(" ")]
S_orig = input()
point_list = []
for target, non_target in zip(["L", "R"], ["R", "L"]):
    S = list(S_orig)

    start_idx_list = []
    before_char = None
    for i, c in enumerate(S):
        if i == 0:
            start_idx_list.append(i)
            before_char = c
        else:
            if before_char == non_target and c == target:
                start_idx_list.append(i)
            before_char = c
    start_idx_list.append(len(start_idx_list) - 1)
    if len(start_idx_list) - 1 <= K:
        point_list.append(len(S) - 1)
        continue

    longest = 0
    start_idx = -1
    end_idx = -1
    for i in range(len(start_idx_list) - K):
        cur_length = start_idx_list[i + K] - start_idx_list[i] + 1
        if cur_length > longest:
            start_idx = start_idx_list[i]
            end_idx = start_idx_list[i + K]
            longest = cur_length

    for _idx in range(start_idx, end_idx):
        S[_idx] = target
    S = ["N"] + S + ["N"]

    point = 0
    for idx in range(1, N + 1):
        if S[idx] == "L":
            if S[idx + 1] == "L":
                point += 1
        if S[idx] == "R":
            if S[idx - 1] == "R":
                point += 1

    point_list.append(point)

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

Fixed code:
N, K = map(int,input().split())
S = input()
ans = 0
for i in range(N-1):
  if S[i] == S[i+1]:
    ans += 1
print(min(N-1, ans + 2 * K))
Buggy code:
N, K = [int(_) for _ in input().split()]
S = input()
now = ''
ans = 0
block = 1
for s in S:
    if now == s:
        ans += 1
    else:
        now = s
        block += 1
if K < block - 1:
    ans += 2 * K
elif K == block - 1:
    ans = N - 2
else:
    ans = N - 1
print(ans)


Fixed code:
N, K = [int(_) for _ in input().split()]
S = input()
now = ''
ans = 0
block = 0
for s in S:
    if now == s:
        ans += 1
    else:
        now = s
        block += 1
block = max(block - 2 * K, 1)
print(N - 1 - (block - 1))
Buggy code:
N,K = map(int,input().split())
S = input()
print(S)

same = 0
diff = 0
for n in range(N-1):
  if S[n] == S[n+1]:
    same += 1
  else:
    diff += 1

ans = same+min(diff,2*K)
print(ans)


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

same = 0
diff = 0
for n in range(N-1):
  if S[n] == S[n+1]:
    same += 1
  else:
    diff += 1

ans = same+min(diff,2*K)
print(ans)
Buggy code:
N, K = map(int, input().split(" "))
S = input()

cnt = []
tmp = 1
for i in range(N - 1):
    if S[i] != S[i + 1]:
        cnt.append(tmp)
        tmp = 1
    else:
        tmp += 1

cnt.append(tmp)
bn = 1 + 2 * K
if len(cnt) <= bn:
    print(N - 1)
    exit(0)

ans = sum(cnt[:bn])
l = len(cnt)
s = ans
for i in range(l - bn):
    s -= cnt[i]
    s += cnt[bn + i]
    ans = max(s, ans)
print(ans)

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

cnt = []
tmp = 1
for i in range(N - 1):
    if S[i] != S[i + 1]:
        cnt.append(tmp)
        tmp = 1
    else:
        tmp += 1

cnt.append(tmp)
bn = 1 + 2 * K
if len(cnt) <= bn:
    print(N - 1)
    exit(0)
ans = sum(cnt) - (len(cnt) - bn + 1)
print(ans)
Buggy code:
def slove():
    import sys
    input = sys.stdin.readline
    n, k = list(map(int, input().rstrip('\n').split()))
    s = str(input().rstrip('\n'))
    c = 0
    cl = 0
    mcl = 0
    cr = 0
    mcr = 0
    for i in range(n):
        if i != 0:
            if s[i] != s[i-1]:
                c += 1
                
        if s[i] == "R":
            cr += 1
            mcl += max((cl - 1), 0)
            cl = 0
        else:
            cl += 1
            mcr += max((cr - 1), 0)
            cr = 0

        if i == n - 1:
            mcl += max((cl - 1), 0)
            mcr += max((cr - 1), 0)

            

if __name__ == '__main__':
    slove()


Fixed code:
def slove():
    import sys
    input = sys.stdin.readline
    n, k = list(map(int, input().rstrip('\n').split()))
    s = str(input().rstrip('\n'))
    c = 0
    cl = 0
    mcl = 0
    cr = 0
    mcr = 0
    for i in range(n):
        if i != 0:
            if s[i] != s[i-1]:
                c += 1
                
        if s[i] == "R":
            cr += 1
            mcl += max((cl - 1), 0)
            cl = 0
        else:
            cl += 1
            mcr += max((cr - 1), 0)
            cr = 0

        if i == n - 1:
            mcl += max((cl - 1), 0)
            mcr += max((cr - 1), 0)

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


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

x = S.count("RL")
y = int(S[0] == "L" or S[-1] == "R")
    
print(min(N - 1, N - (x * 2 + y) + K * 2))


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

x = S.count("RL")
y = int(S[0] == "L") + int(S[-1] == "R")
    
print(min(N - 1, N - (x * 2 + y) + K * 2))
Buggy code:
n, k = map(int, input().split())
s = list(input())
s_num = []
point = 1
prev = s[0]
ans = 0
for i in range(n - 1):
    if prev != s[i + 1]:
        prev = s[i + 1]
        s_num.append(point)
        ans += point - 1
        point = 1        
    else:
        point += 1
s_num.append(point)
ans += point - 1
kkk = max(len(s_num) - 2, 0)
ans = ans + min(kkk, k) * 2
if kkk < k:
    ans += k - kkk
ans = max(n, ans)
print(ans)

Fixed code:
n, k = map(int, input().split())
s = list(input())
s_num = []
point = 1
prev = s[0]
ans = 0
for i in range(n - 1):
    if prev != s[i + 1]:
        prev = s[i + 1]
        s_num.append(point)
        ans += point - 1
        point = 1        
    else:
        point += 1
s_num.append(point)
ans += point - 1
kkk = max(len(s_num) - 2, 0)
ans = ans + min(kkk, k) * 2
if kkk < k:
    ans += k - kkk
ans = min(n - 1, ans)
print(ans)
Buggy code:
N, K = map(int, input().split())
S = list(map(str, input()))
score = 0
for i in range(1, N):
	if S[i] == S[i + 1]:
		score += 1
print(min(score + 2 * K, N - 1))

Fixed code:
N, K = map(int, input().split())
S = list(map(str, input()))
score = 0
for i in range(N - 1):
	if S[i] == S[i + 1]:
		score += 1
print(min(score + 2 * K, N - 1))
Buggy code:
a,b = map(int,input().split())
S = input()
tmp = S[0]
cnt = 0
L = []
for x in S:
    if tmp == x:
        cnt += 1
    else:
        L.append(cnt)
        cnt = 1
        tmp = x
L.append(cnt)
tmp = 0
for _ in range(b):
    for _ in range(2):
        tmp += L.pop(0)
tmp += L.pop(0)
L = [tmp] + L
print(sum(L) - len(L))


Fixed code:
a,b = map(int,input().split())
S = input()
tmp = S[0]
cnt = 0
L = []
for x in S:
    if tmp == x:
        cnt += 1
    else:
        L.append(cnt)
        cnt = 1
        tmp = x
L.append(cnt)
tmp = 0
for _ in range(b):
    for _ in range(2):
        try:
            tmp += L.pop(0)
        except:
            print(a- 1)
            quit()
try:
    tmp += L.pop(0)
except:
    print(a-1)
    quit()
L = [tmp] + L
print(sum(L) - len(L))
Buggy code:
N, K = map(int, input().split())
S = input()
counter = 0

ans = N-1

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

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

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

ans = N-1

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

print(min(N-1, ans+2*K))
Buggy code:
N, K = map(int, input().split())
S = input()
counter = 0
RL = 0
RL_L = 0
RL_R = 0

if S[0] == 'L':
    RL_L += 1

if S[N-1] == 'R':
    RL_R += 1

for i in range(N-1):
    if S[i:i+2] == 'RL':
        counter += 1

if (counter - K) >= 0:
    counter -= K
    K = 0
else:
    K  -= counter
    counter = 0
    RL = 0

if RL_L == 1 and K > 0:
    RL_L -= 1
    K -= 1

if RL_R == 1 and K > 0:
    RL_R -= 1
    K -= 1

ans = N - (RL-counter*2 + RL_L + RL_R)

print(ans)

Fixed code:
N, K = map(int, input().split())
S = input()
counter = 0
RL = 0
RL_L = 0
RL_R = 0

if S[0] == 'L':
    RL_L += 1

if S[N-1] == 'R':
    RL_R += 1

for i in range(N-1):
    if S[i:i+2] == 'RL':
        counter += 1

if (counter - K) >= 0:
    counter -= K
    K = 0
else:
    K -= counter
    counter = 0
    RL = 0

if RL_L == 1 and K > 0:
    RL_L -= 1
    K -= 1

if RL_R == 1 and K > 0:
    RL_R -= 1
    K -= 1

ans = N - (counter*2 + RL_L + RL_R)

print(min(ans, N-1))
Buggy code:
N, K = map(int, input().split())
S = input()

happiness = 0

for word in S.split("L"):
    happiness += max(0, len(word) - 1)
for word in S.split("R"):
    happiness += max(0, len(word) - 1)

res = happiness + 2 * K
print(min(res, N))

Fixed code:
N, K = map(int, input().split())
S = input()
 
happiness = 0
 
for word in S.split("L"):
    happiness += max(0, len(word) - 1)
for word in S.split("R"):
    happiness += max(0, len(word) - 1)
 
res = happiness + 2 * K
print(min(res, N-1))
Buggy code:
N,K = map(int,input().split())
B = input()

happy = 0
for n in range(N - 1):
     if B[n] == B[n + 1]:
            happy += 1

print(N -1 if happy + K*2 > n-1 else happy + K*2)
    

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

happy = 0
for n in range(N - 1):
     if B[n] == B[n + 1]:
            happy += 1

print(N -1 if happy + K*2 > N-1 else happy + K*2)
Buggy code:
import heapq
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')

N, K = LI()
S = str(input())
ans = int(0)

R = []
L = []
before = "R"
if S[0]=="L":
    R.append(0)
    before = "L"
cur = int(0)
for s in S:
    if before==s:
        cur +=1
    else:
        if s=="L":
            R.append(cur)
            before = "L"
            cur = int(1)
        else:
            L.append(cur)
            before = "R"
            cur = int(1)

if s == "L":
    L.append(cur)
else:
    R.append(cur)

#print(R,L)
Rlen = len(R)
Llen = len(L)
#ansC = []
#for i in range(Rlen-K):
#    cur = int(0)
#    for j in range(K+1):
#        cur += R[i+j]
#    for j in range(K):
#        cur += L[i+j]
#    ansC.append(cur)
#for i in range(Llen-K):
#    cur = int(0)
#    for j in range(K):
#       cur += R[i+j+1]
#    for j in range(K+1):
#        cur += L[i+j]
#    ansC.append(cur)
#print(ansC)

ans = min(N-1, 2*K+sum(R)+sum(L)-Rlen-Llen)
if S[0]=="L":
    ans +=1
print(ans)


Fixed code:
import heapq
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')

N, K = LI()
S = str(input())
ans = int(0)

R = []
L = []
before = "R"
if S[0]=="L":
    R.append(0)
    before = "L"
cur = int(0)
for s in S:
    if before==s:
        cur +=1
    else:
        if s=="L":
            R.append(cur)
            before = "L"
            cur = int(1)
        else:
            L.append(cur)
            before = "R"
            cur = int(1)

if s == "L":
    L.append(cur)
else:
    R.append(cur)

#print(R,L)
Rlen = len(R)
Llen = len(L)
#ansC = []
#for i in range(Rlen-K):
#    cur = int(0)
#    for j in range(K+1):
#        cur += R[i+j]
#    for j in range(K):
#        cur += L[i+j]
#    ansC.append(cur)
#for i in range(Llen-K):
#    cur = int(0)
#    for j in range(K):
#       cur += R[i+j+1]
#    for j in range(K+1):
#        cur += L[i+j]
#    ansC.append(cur)
#print(ansC)

ans = min(N-1, 2*K+sum(R)+sum(L)-Rlen-Llen)
if S[0]=="L":
    ans +=1
ans = min(N-1,ans)
print(ans)