Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def conb(n,r): return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


n,k = inpl()
s = input()
cntlr = []
tmp = ''
cnt = 0
for i in range(n):
    if i == 0:
        tmp = s[i]
        cnt = 1
    else:
        if tmp == s[i]:
            cnt += 1
        else:
            cntlr.append(cnt)
            tmp = s[i]
            cnt = 1
cntlr.append(cnt)
# print(cntlr)
nn = len(cntlr)
if nn == 1:
    print(n-1)
    quit()
if nn%2==0:
    for i in range(k):
        if len(cntlr) > 4:
            cntlr[0] += cntlr[len(cntlr)-3]
            cntlr[len(cntlr)-2] += cntlr[1]
            del(cntlr[len(cntlr)-3])
            del(cntlr[1])
            for k in range((len(cntlr)-3)//2):
                cntlr[k+1],cntlr[len(cntlr)-3-k]=cntlr[len(cntlr)-3-k],cntlr[k+1]
        elif len(cntlr) ==  4:
            cntlr[0] += cntlr[1] + cntlr[2]
            del(cntlr[1:3])
        else:
            print(n-1)
            quit()
else:
    for i in range(k):
        if len(cntlr) > 3:
            cntlr[0] += cntlr[len(cntlr)-2]
            cntlr[-1] += cntlr[1]
            del(cntlr[len(cntlr)-2])
            del(cntlr[1])
            for k in range((len(cntlr)-2)//2):
                cntlr[k+1],cntlr[-2] = cntlr[-2],cntlr[k+1]
        else:
            print(n-1)
            quit()
res = 0
for i in cntlr:
    if i >= 2:
        res += i-1
print(res)


Fixed submission:
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def conb(n,r): return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


n,k = inpl()
s = input()
cnt = 0
for i in range(n-1):
    if s[i] == s[i+1]:
        cnt += 1
print(min(cnt+2*k, n-1))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def conb(n,r): return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


n,k = inpl()
s = input()
cntlr = []
tmp = ''
cnt = 0
for i in range(n):
    if i == 0:
        tmp = s[i]
        cnt = 1
    else:
        if tmp == s[i]:
            cnt += 1
        else:
            cntlr.append(cnt)
            tmp = s[i]
            cnt = 1
cntlr.append(cnt)
# print(cntlr)
nn = len(cntlr)
if nn == 1:
    print(n-1)
    quit()
if nn%2==0:
    for i in range(k):
        if len(cntlr) > 4:
            cntlr[0] += cntlr[len(cntlr)-3]
            cntlr[len(cntlr)-2] += cntlr[1]
            del(cntlr[len(cntlr)-3])
            del(cntlr[1])
            for k in range((len(cntlr)-3)//2):
                cntlr[k+1],cntlr[len(cntlr)-3-k]=cntlr[len(cntlr)-3-k],cntlr[k+1]
        elif len(cntlr) ==  4:
            cntlr[0] += cntlr[1] + cntlr[2]
            del(cntlr[1:3])
        else:
            print(n-1)
            quit()
else:
    for i in range(k):
        if len(cntlr) > 3:
            cntlr[0] += cntlr[len(cntlr)-2]
            cntlr[-1] += cntlr[1]
            del(cntlr[len(cntlr)-2])
            del(cntlr[1])
            for k in range((len(cntlr)-2)//2):
                cntlr[k+1],cntlr[-2] = cntlr[-2],cntlr[k+1]
        else:
            print(n-1)
            quit()
res = 0
for i in cntlr:
    if i >= 2:
        res += i-1
print(res)


Fixed submission:
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def conb(n,r): return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


n,k = inpl()
s = input()
cnt = 0
for i in range(n-1):
    if s[i] == s[i+1]:
        cnt += 1
print(min(cnt+2*k, n-1))