Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split(' '))
S = input()
previous_idx = 0
if S[0] == 'L':
    search_s = 'R'
    non_search_s = 'L'
else:
    search_s = 'L'
    non_search_s = 'R'

flag  = False
i = 0
count_s = 0
new_S = ''
previous_s = S[0]
while K != 0 and i != N:
    if previous_s == S[i]:
        count_s +=1
        if i+1 == N:
            flag =True
            new_S += non_search_s*count_s
    else:
        if previous_s == search_s:
            new_S += non_search_s*count_s
            K -= 1
        else:
            new_S += non_search_s*count_s
        previous_s = S[i]
        count_s = 1
    i += 1
if not flag:
    new_S += S[i-1:N]
#print(new_S)
'''
while K != 0 and idx != N:
    if S[idx] == search_s:
        if not continue_flag:
            continue_flag = True
        new_S += non_search_s
    else:
        if continue_flag:
            continue_flag = False
            K -= 1
            last_idx = idx
        new_S += S[idx]
    idx += 1
new_S += S[idx:N]
#print(new_S)
'''
count_s = 0
previous_s = new_S[0]
for i in range(1,N):
    if previous_s == new_S[i]:
        count_s +=1
    else:
        previous_s = new_S[i]
print(count_s)

Fixed submission:
N,K = map(int,input().split(' '))
S = input()
previous_idx = 0
if S[0] == 'L':
    search_s = 'R'
    non_search_s = 'L'
else:
    search_s = 'L'
    non_search_s = 'R'

flag  = False
i = 0
count_s = 0
new_S = ''
previous_s = S[0]
while K != 0 and i != N:
    if previous_s != S[i]:
        if previous_s == search_s:
            #new_S += non_search_s*count_s
            K -= 1
        #else:
            #new_S += non_search_s*count_s
        previous_s = S[i]
        #count_s = 1
        #count_s += 1
        #if i+1 == N:
        #    flag =True
            #new_S += non_search_s*count_s
        
    i += 1
if K > 0:
    print(N-1)
else:
    new_S += non_search_s*(i-1)
    if not flag:
        new_S += S[i-1:N]
    #print(new_S)
    count_s = 0
    previous_s = new_S[0]
    for i in range(1,N):
        if previous_s == new_S[i]:
            count_s +=1
        else:
            previous_s = new_S[i]
    print(count_s)

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split(' '))
S = input()
previous_idx = 0
if S[0] == 'L':
    search_s = 'R'
    non_search_s = 'L'
else:
    search_s = 'L'
    non_search_s = 'R'

continue_flag = False
idx = 0
lst_idx =  -5
new_S = ''
while K != 0 and idx != N:
    if S[idx] == search_s:
        if not continue_flag:
            continue_flag = True
        new_S += non_search_s
    else:
        if continue_flag:
            continue_flag = False
            K -= 1
            last_idx = idx
        new_S += S[idx]
    idx += 1
new_S += S[idx:N]
#print(new_S)

count_s = 0
previous_s = new_S[0]
for i in range(1,N):
    if previous_s == new_S[i]:
        count_s +=1
    else:
        previous_s = new_S[i]
print(count_s)


Fixed submission:
N,K = map(int,input().split(' '))
S = input()
previous_idx = 0
if S[0] == 'L':
    search_s = 'R'
    non_search_s = 'L'
else:
    search_s = 'L'
    non_search_s = 'R'

flag  = False
i = 0
count_s = 0
new_S = ''
previous_s = S[0]
while K != 0 and i != N:
    if previous_s != S[i]:
        if previous_s == search_s:
            #new_S += non_search_s*count_s
            K -= 1
        #else:
            #new_S += non_search_s*count_s
        previous_s = S[i]
        #count_s = 1
        #count_s += 1
        #if i+1 == N:
        #    flag =True
            #new_S += non_search_s*count_s
        
    i += 1
if K > 0:
    print(N-1)
else:
    new_S += non_search_s*(i-1)
    if not flag:
        new_S += S[i-1:N]
    #print(new_S)
    count_s = 0
    previous_s = new_S[0]
    for i in range(1,N):
        if previous_s == new_S[i]:
            count_s +=1
        else:
            previous_s = new_S[i]
    print(count_s)