Problem:
 Score : 400 points    Problem Statement There are N people standing in a queue from west to east.  Given is a string S of length N representing the directions of the people.
The i -th person from the west is facing west if the i -th character of S is L , and east if that character of S is R .  A person is happy if the person in front of him/her is facing the same direction.
If no person is standing in front of a person, however, he/she is not happy.  You can perform the following operation any number of times between 0 and K (inclusive):  Operation: Choose integers l and r such that 1 \leq l \leq r \leq N , and rotate by 180 degrees the part of the queue: the l -th, (l+1) -th, ... , r -th persons. That is, for each i = 0, 1, ..., r-l , the (l + i) -th person from the west will stand the (r - i) -th from the west after the operation, facing east if he/she is facing west now, and vice versa.  What is the maximum possible number of happy people you can have?      Constraints  N is an integer satisfying 1 \leq N \leq 10^5 .  K is an integer satisfying 1 \leq K \leq 10^5 .  |S| = N  Each character of S is L or R .         Input Input is given from Standard Input in the following format:  N  K  S       Output Print the maximum possible number of happy people after at most K operations.        Sample Input 1 6 1
LRLRRL      Sample Output 1 3  If we choose (l, r) = (2, 5) , we have LLLRLL , where the 2 -nd, 3 -rd, and 6 -th persons from the west are happy.       Sample Input 2 13 3
LRRLRLRRLRLLR      Sample Output 2 9       Sample Input 3 10 1
LLLLLRRRRR      Sample Output 3 9       Sample Input 4 9 2
RRRLRLRLL      Sample Output 4 7

Solution (Python):
n, k = map(int, input().split())
s = list(input())

ini = 0
for i in range(n - 1):
	if s[i] == s[i + 1]:
		ini += 1

res = min(n - 1, ini + (k * 2))
print(res)

Solution (Python):
n, k = map(int,input().split())
s = input()
i = 0
while len(s) > i + 1:
    if s[i] == s[i + 1]:
        s = s[:i] + s[i + 1:]
    else:
        i += 1

ans = len(s)

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

ans = n - ans

print(ans)

Solution (Python):
from sys import stdin
from itertools import groupby

n,k = [int(x) for x in stdin.readline().rstrip().split()]
s = stdin.readline().rstrip()

kohuku = 0

for i in range(len(s)-1):
    if s[i] == "R" and s[i+1] == "R":
        kohuku += 1
    elif s[i] == "L" and s[i+1] == "L":
        kohuku += 1

if len(s) == 1:
    print(0)
    exit()

num_2 = s.count("RL")

num_1 = 0
if s[0] == "R":
    num_1 += 1
elif s[-1] == "R":
    num_1 += 1
if k <= num_2:
    print(min(n-1,2*k+kohuku))
    exit()

if num_1 == 0:
    print(min(n-1,2*k+kohuku))

elif num_1 == 1:
    print(min(n-1,2*num_2+1+kohuku))

else:
    if k == num_2 +1:
        print(min(n-1,2*num_2+1+kohuku))
    else:
        print(min(n-1,2*num_2+2+kohuku))

Solution (Python):
from sys import stdin
from itertools import groupby

n,k = [int(x) for x in stdin.readline().rstrip().split()]
s = stdin.readline().rstrip()

kohuku = 0

for i in range(len(s)-1):
    if s[i] == "R" and s[i+1] == "R":
        kohuku += 1
    elif s[i] == "L" and s[i+1] == "L":
        kohuku += 1

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

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

def ranlength(A):
    num = list(A)
    count = 1
    table = []
    for i in range(len(num)):
        try:
            if num[i] == num[i+1]:
               count = count + 1
            else:
                table.append(count)
                count = 1
        except:
            table.append(count)
    return(table)

RL=ranlength(S)
a=0
for i in RL:
    a+=i-1

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

Solution (Python):
import sys
MAX_INT = int(10e12)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N, K = IL()
s = S()

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)

Solution (Python):
N, K = map(int, input().strip().split())
S = input().strip()
cnt = 0
for i in range(1, N):
  if S[i-1] == S[i]:
    cnt += 1

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

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

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

happy_face = N - 1 - S.count('LR') - S.count('RL')
ans = min(N - 1, happy_face + 2 * K)
print(ans)

Solution (Python):
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(len(s)-1,ans+2*k))

Solution (Python):
n,k=map(int,input().split())
S=input()
i=0
a=0
for i in range(n-1):
  if S[i]==S[i+1]:
    a=a+1
    i=i=1
  else:
    i=i+1
for j in range(k):
  if a<=(n-3):
    a=a+2
    k=k+1
  elif a==(n-2):
    a=a+1
    k=k+1
  else:
    k=k+1
print(a)

Solution (Python):
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)


Rlen = len(R)
Llen = len(L)

















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)

Solution (Python):
N,K = map(int,input().split(' '))
S = input()




happies = 0
happy_max = N-1

for i in range(N-1):
    if (S[i] == 'R') & (S[i+1] == 'R'):
        happies +=1
    if (S[-i-1] == 'L') & (S[-i-2] == 'L'):
        happies +=1
print(min(happies+2*K,happy_max))

Solution (Python):
def main():
    n,k=map(int,input().split())
    a=input()
    i=0
    cnt=1
    l=[]
    while i<n-1:
        if a[i]!=a[i+1]:
            l.append(cnt)
            cnt=1
        else:
            cnt+=1
        i+=1
    l.append(cnt)
    print(min(n-len(l)+2*k,n-1))
main()

Solution (Python):
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 = min(h+2*k,n-1)
print(ans)

Solution (Python):
n, k = map(int, input().split())

s = input()

cnt = 0

for x in range(n - 1) :
    if s[x] == s[x + 1] :
        cnt += 1

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

Solution (Python):
N, K = map(int, input().split())
S = input()
partition = 1
for a, b in zip(S, S[1:]):
    if a != b:
        partition += 1

print(min(N - 1, N - (partition - (2 * K))))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
n,k = map(int,input().split())
s = input()

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

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

Solution (Python):
import sys


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n, k = list(map(int, input().rstrip('\n').split()))
    s = str(input().rstrip('\n'))
    ls = []
    cnt = 1
    for i in range(1, n):
        if s[i] != s[i-1]:
            ls.append(cnt-1)
            cnt = 1
        else:
            cnt += 1
    ls.append(cnt-1)
    print(min(n-1, sum(ls) + k * 2))


if __name__ == '__main__':
    solve()

Solution (Python):
n,k=map(int, input().split()) 
s=list(input())
s=list('R')+s+list('L')
count1=0
count2=0

for i in range(1,n+2):
    if s[i-1]=='R' and s[i]=='L':
        if i==1 or i==n+1:
            count1+=1
        else:
            count2+=1
            
ans=n-(count1+count2*2)

if k<=count2:
    print(ans+k*2)
else:
    print(n-1)

Solution (Python):

N, K = [int(item) for item in input().split()]
S = [item for item in input()]

def compress(arr):
    """圧縮して消えた人はhappy"""
    cnt_h = 0
    new_arr = []
    comp_arr = ['L']

    
    if arr[0] == 'R':
        for item in arr:
            if item == 'L':
                new_arr.append('R')
            else:
                new_arr.append('L')

            prev_item = item
    else:
        new_arr = arr
    

    for i in range(1, N):
        if new_arr[i - 1] == new_arr[i]:
            cnt_h += 1
        else:
            comp_arr.append(new_arr[i])

    return [comp_arr, cnt_h] 


def execute(arr, cnt_h, K):
    
    max_rotation = len(arr)//2
    
    if max_rotation <= K:
        cnt_h += len(arr) - 1
    else:
        cnt_h += 2*K

    return cnt_h

arr, cnt_h = compress(S)
print(execute(arr, cnt_h, K))

Solution (Python):
N, K = [int(item) for item in input().split()]
S = [item for item in input()]

def compress(arr):
    """圧縮して消えた人はhappy"""
    cnt_h = 0
    new_arr = []
    comp_arr = ['L']

    
    if arr[0] == 'R':
        for item in arr:
            if item == 'L':
                new_arr.append('R')
            else:
                new_arr.append('L')

            prev_item = item
    else:
        new_arr = arr
    

    for i in range(1, N):
        if new_arr[i - 1] == new_arr[i]:
            cnt_h += 1
        else:
            comp_arr.append(new_arr[i])

    return [comp_arr, cnt_h] 


def execute(arr, cnt_h, K):
    
    max_rotation = len(arr)//2
    
    if max_rotation <= K:
        if arr[-1] == 'R':
            cnt_h += max_rotation*2 -1
        elif arr[-1] == 'L':
            cnt_h += max_rotation*2
    else:
        if arr[2*K] == 'R':
            cnt_h += 2*K - 1
        elif arr[2*K] == 'L':
            cnt_h += 2*K

    return cnt_h

arr, cnt_h = compress(S)
print(execute(arr, cnt_h, K))

Solution (Python):
n,m = map(int,input().split())
list1 = list(input())

first,last = 0,0 
count = 0
flag = 1 


for i in range(m+1): 
	if first == n:
		break
	while True:
		if first == n:
			break
		if list1[first] != list1[last]:
			if flag:
				last = first
				first += 1
				flag = 0
				break
			else:
				flag = 1
				last = first
				if i == m:
					first += 1
					break 
				
				
		first += 1

if first == n: 
	print(n-1)
	exit()



count += last -1 +1 



while True:
	if first == n:
		break
	while True:
		if first == n:
			count += first - last -1 
			break
		if list1[first] != list1[last]:
			count += first - last -1
			last = first
			first += 1
			break
		first += 1

print(count)

Solution (Python):
def main():

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


    num = 0
    for i in range(len(b)-1):
        if b[i] != b[i+1]:
            num += 1
    print(min(N-1, N-1-num+2*K))

if __name__ == '__main__':
    main()

Solution (Python):



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


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


happiness = happiness + 2 * K
if N - 1 < happiness:
    happiness = N - 1


print(happiness)

Solution (Python):
if __name__ == '__main__':
    N, K = map(int, input().split())
    string = input()
    count = 0
    for idx in range(N - 1):
        if string[idx] == string[idx + 1]: count += 1
    print(min(N - 1, count + 2 * K))

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

Solution (Python):
N,K = map(int, input().split())
S=input()
RL=S.count('RL')
LR=S.count('LR')
diff=RL+LR
happy=N-1-diff
if diff%2==0:
    print(happy+2*min(diff//2,K))
else:
    print(happy+2*K if (diff//2+1)>K else happy+diff)

Solution (Python):
N, K = map(int, input().split())
s = input()

RL = s.count('RL')
LR = s.count('LR')
diff = RL + LR

cnt = (N-1) - diff

for i in range(K):
    if diff >= 2:
        diff -= 2
        cnt += 2
    elif diff == 1:
        diff -= 1
        cnt += 1

print(cnt)

Solution (Python):
n, k = list(map(int, input().split()))
s = input()
group = 1



for c, d in zip(s, s[1:]):
	if c != d:
		group += 1
	
	
	
print(min(n - 1, n - (group - 2 * k)))

Solution (Python):
n, k = map(int, input().split())
s = input()

RL = s.count("RL")
KADO = 0
if s[0] == "L":
    KADO += 1
if s[-1] == "R":
    KADO += 1

if k < RL:
    RL -= k
elif k > RL and KADO == 2:
    RL = 0
    KADO -= 1
else:
    RL = 0

if RL + KADO == 0:
    KADO =1

print(n-(RL*2 + KADO))

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

Solution (Python):
n, k = map(int, input().split())
s = input()
pre = 0
sepS = []
happyCount = 0
RLcount = 0
for i in range(1, n):
    if s[i] == s[i - 1]:
        happyCount += 1
    if s[i] == 'L' and s[i - 1] == 'R':
        RLcount += 1

happyCount += min(k, RLcount) * 2
if k > RLcount:
    happyCount += 1
print(min(n - 1, happyCount))

Solution (Python):
N, K = [int(x) for x in input().split()]
S = str(input())

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

Solution (Python):
N, K = [int(x) for x in input().split()]
S = str(input())

Unhappy = {}
Unhappy[1] = 0
Unhappy[2] = 0

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

if Unhappy[1] == 0:
    act = min(K, Unhappy[2] - 1)
    K -= act
    Unhappy[2] -= act
    if K != 0:
        K -= 1
        Unhappy[2] -= 1
        Unhappy[1] += 1
else:
    act = min(K, Unhappy[2])
    K -= act
    Unhappy[2] -= act
    if Unhappy[1] == 2 and K != 0:
        K -= 1
        Unhappy[1] -= 1

print(N - Unhappy[1] - Unhappy[2] * 2)

Solution (Python):
N, K = [int(_) for _ in input().split()]
S = list(input())

def flip(l, r):
    for i in range((r + l) // 2 - l + 1):
        tmp = S[l + i]

        S[l + i] = 'R' if S[r - i] == 'L' else 'L'
        S[r - i] = 'R' if tmp == 'L' else 'L'


def f(w, start):
    l_idx = -1
    for i in range(start, N - 1):
        if S[i] == w[0] and S[i + 1] == w[1]:
            l_idx = i
            break

    if l_idx < 0: return -1
    r_idx = -1

    for i in range(l_idx + 1, N - 1):
        if S[i] == w[1] and S[i + 1] == w[0]:
            r_idx = i
            break
    if r_idx < 0: return -1

    flip(l_idx + 1, r_idx)
    return l_idx + 1


cnt = 0
for LR in ['LR', 'RL']:
    l = 0
    while cnt < K and l >= 0 and l < N - 1:
        l = f(LR, l)
        if l >= 0: cnt += 1

l = 0
for i in range(N - 1):
    if cnt >= K: break

    if S[i] != S[i + 1]:
        flip(l, i)

m = 0
spl = 0
for i in range(N - 1):
    if S[i] != S[i + 1]:
        spl += 1
print(N - spl - 1)

Solution (Python):
N, K = [int(_) for _ in input().split()]
S = list(input())

cnt = 0
l = 1

def f(w, start):
    l_idx = -1
    
    for i in range(start, N - 1):
        if S[i] == w[0] and S[i + 1] == w[1]:
            l_idx = i
            break

    if l_idx < 0: return -1
    r_idx = -1

    for i in range(l_idx + 1, N - 1):
        if S[i] == w[1] and S[i + 1] == w[0]:
            r_idx = i
            break

    if r_idx < 0: return -1

    for i in range(l_idx + 1, r_idx + 1):
        S[i] = 'R' if S[i] == 'L' else 'L'

    return l_idx + 1


cnt = 0

l = 1

while cnt < K and l > 0 and l < N - 1:
    l = f('LR', l)
    if l > 0: cnt += 1

l = 1
while cnt < K and l > 0 and l < N - 1:
    l = f('RL', l)
    if l > 0: cnt += 1

l = 0
for i in range(N - 1):
    if cnt >= K: break

    if S[i] != S[i + 1]:
        for j in range(l,i+1):
            S[j] = 'R' if S[j] == 'L' else 'L'
        cnt += 1
        l=i+1

m = 0
pre = ''
ans = 0
for i in range(N):
    if pre == S[i]:
        m += 1
    else:
        ans += m
        m = 0
        pre = S[i]
ans += m
print(ans)

Solution (Python):
N,K=map(int,input().split())
H=input()
a=0
for i in range(N-1):
 if H[i]==H[i+1]:
  a+=1
b=min(N-1,a+2*K)
print(b)

Solution (Python):

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



happy = 0

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



if happy + 2*K < N-1:
    print(happy + 2*K)
else:
    print(N-1)

Solution (Python):
N, K =map(int, input().split())
A = input()

tmp = A[0]
cnt = 1
for i in range(1,N):
	if A[i] == tmp:
		continue
	else:
		tmp = A[i]
		cnt += 1

ans = N - (cnt - (2*K))

print(min(N-1, ans))

Solution (Python):
n, k = map(int, input().split())
lr = list(input())
j = 0
counts = [0]
for i in range(n):
    if lr[j] == lr[i]:
        counts[-1] += 1
    else:
        j = i
        counts.append(1)

for _ in range(k):
    if len(counts)>=3:
        counts[0] += counts[1]+counts[2]
        del counts[1:3]
    elif len(counts)==2:
        counts[0] += counts[1]
        del counts[1]
    else:
        break

count = sum(counts) - len(counts)
print(count)

Solution (Python):
n, k = map(int, input().split())
s = input()

prev = s[0]
line = []
cnt = 1
for v in s[1:]:
    if v != prev:
        prev = v
        line.append(cnt - 1)
        cnt = 1
    else:
        cnt += 1
line.append(cnt - 1)

k = min(len(line) // 2, k)
print(sum(line) + k * 2 - (len(line) // 2 == k and len(line) % 2 == 0))

Solution (Python):
import sys
from collections import deque

input = sys.stdin.readline

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

sq = deque(S)
lst=[]
j=0

for i in range(K):
    j=0
    if not sq:break
    sq_1st = sq.popleft()
    sq_2nd = sq_1st
    lst.append(sq_1st)

    while sq:
        sq_3rd = sq.popleft()
        if sq_2nd!=sq_3rd:
            j+=1
            sq_2nd=sq_3rd
            if j==2:
                sq.appendleft(sq_3rd)
                break
        lst.append(sq_1st)

lst+=list(sq)             

def eval(N,S):
    total=0
    for i in range(1,N):
        if S[i]==S[i-1]:total+=1
    return total
  
print(eval(N,lst))

Solution (Python):
n, k = map(int, input().split())
s = input()

if s[0] == 'L' and s[n-1] == 'L':
    s = 'R' + s
    cnt = 0
    for i in range(n):
        if s[i] == 'R' and s[i+1] == 'L':
            cnt += 1
    if cnt <= k:
        print(n-1)
    elif cnt == k+1:
        print(n-1)
    else:
        print(n-2*(cnt-1)-1+2*k)

elif s[0] == 'L' and s[n-1] == 'R':
    s = 'R' + s + 'L'
    cnt = 0
    for i in range(n+1):
        if s[i] == 'R' and s[i+1] == 'L':
            cnt += 1
    if cnt <= k:
        print(n-1)
    elif cnt == k+1:
        print(n-1)
    elif cnt == k+2:
        print(n-2)
    else:
        print(n-2*(cnt-2)-2+2*k)

elif s[0] == 'R' and s[n-1] == 'L':
    cnt = 0
    for i in range(n-1):
        if s[i] == 'R' and s[i+1] == 'L':
            cnt += 1
    if cnt <= k:
        print(n-1)
    else:
        print(n-2*cnt+2*k)

elif s[0] == 'R' and s[n-1] == 'R':
    s = s + 'L'
    cnt = 0
    for i in range(n):
        if s[i] == 'R' and s[i+1] == 'L':
            cnt += 1
    if cnt <= k:
        print(n-1)
    elif cnt == k+1:
        print(n-1)
    else:
        print(n-2*(cnt-1)-1+2*k)

Solution (Python):
N, K = map(int, input().split())
A = list(map(str, input()))

tmp = A[0]
cnt = 1
for i in range(1,N):
	if A[i] == tmp:
		if i != N-1:
			continue

	else:
		cnt += 1
		tmp = A[i]
		
t = cnt - (2*K)

if t > 0:
	ans = N-t
else:
	ans = N-1

print(ans)

Solution (Python):
import sys
n, k = map(int, input().split())
s = list(input())
left = 0
right = 0
if s[0] == "L":
    left = 1
if s[n-1] == "R":
    right = 1
count = 0
if n == 1:
    print(0)
    sys.exit()
for i in range(n-1):
    if s[i] == "R" and s[i+1] == "L":
        count += 1
if k < count:
    count -= k
    ans = n - count*2 - left - right
    print(ans)
elif k == count:
    if left == right == 1:
        print(n-2)
    else:
        print(n-1)
else:
    print(n-1)

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

Solution (Python):
n,k = map(int,input().split())
s = list(input())
x = 0
y = 0
if s[0] == "L":
    y += 1
if s[n-1] == "R":
    y += 1 
for i in range(n-1):
    if s[i] == "R" and s[i+1] =="L":
        x += 1
if x >= k:
    print(n-(x-k)*2-y)
else:
    print(n-1)

Solution (Python):
import sys
n, k = map(int, input().split())
s = list(input())
left = 0
right = 0
if s[0] == "L":
    left = 1
if s[n-1] == "R":
    right = 1
count = 0
if n == 1:
    print(0)
    sys.exit()
for i in range(n-1):
    if s[i] == "R" and s[i+1] == "L":
        count += 1
if k < count:
    count -= k
    ans = n - count*2 - left - right
    print(ans)
elif k == count:
    if left == right == 1:
        print(n-2)
    else:
        print(n-1)
else:
    print(n-1)

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

LR = []
num = 1
now = S[0]

for i in range(1,N):
    if S[i] == now:
        num += 1
    else:
        now = S[i]
        LR.append(num)
        num = 1

LR.append(num)

length = len(LR)
print(N-length+min(2*K,length-1))

Solution (Python):
import itertools
N, K = map(int, input().split())
S = input()
A = list()
c = 0
for n in range(len(S)-1):
    if S[n] == S[n+1]:
        c += 1
print(min(c+2*K, N-1))

Solution (Python):
n,k = map(int, input().split())
s = list(input())

r = 0
happy = 0

for i in range(1, len(s)):
    if s[i-1] is not s[i]:
        if k is not 0:
            if r is 0:
                r = 1
            else:
                happy += 2
                r = 0
                k -= 1
    else:
        happy += 1

if r == 1:
    happy += 1
print(happy)

Solution (Python):
def main():
    N, K = map(int, input().split())
    S = input()
    cnt = 0
    for i in range(N-1):
        if S[i] == 'R' and S[i] == S[i+1]:
            cnt += 1

    for i in range(N-1,0,-1):
        if S[i] == 'L' and S[i] == S[i-1]:
            cnt += 1

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

if __name__ == "__main__":
    main()

Solution (Python):
N, K = map(int, input().split())
S = input()
print(N - 1 - max(0,  len([0 for i in range(1, N) if S[i] != S[i-1]]) - 2 * K))

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

flips = sum(int(x!=y) for x,y in zip(S,S[1:]))

flips = max(0,flips-2*K)
print(N-1-flips)

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

Solution (Python):

import sys


def solve(N: int, K: int, S: str):
    rep_ss = []
    check_c = S[0]
    cnt = 0
    for char in S:
        if check_c == char:
            cnt += 1
        else:
            rep_ss.append(cnt)
            cnt = 1
            check_c = char
    rep_ss.append(cnt)
    adj = 0
    if K >= len(rep_ss)//2:
        K = len(rep_ss) // 2
        if len(rep_ss) % 2 == 0:
            K -= 1
            adj = 1
    ans = N - len(rep_ss) + 2 * K + adj
    print(ans)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    S = next(tokens)  
    solve(N, K, S)


if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split())
S = list( input() )

if len(S) > 1:
    count = 0

    temp = S[1]

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

    print( min(len(S)-1, count + 2*K) )
else:
    print(0)

Solution (Python):
from itertools import*

a,b=map(int,input().split())
s=input()
n=[len(list(v)) for k,v in groupby(s)]
print(min(sum(n)-len(n)+min(len(n)-1,b)*2,a-1))

Solution (Python):

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

same, other = 0, 0
for i in range(N - 1):
    if S[i] == S[i + 1]:
        same += 1
    else:
        other += 1

if (other + 1) // 2 <= K:
    print(same + other)
else:
    print(same + 2 * K)

Solution (Python):
import re
N, K = map(int, input().split())
S = input()
s_arr = list(map(len, re.findall(r'R+|L+',S)))
not_happies = len(s_arr)
happys = not_happies - K
if (not_happies-1)/2 <= K:
    ans = N-1
else:
    ans = N - (not_happies - 2*K)
print(ans)

Solution (Python):
N, K = map(int, input().split())
S = list(input())

hikkuri = 0

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

shiawase = len(S) - hikkuri - 1

for i in range(K):
    if hikkuri >= 2:
        shiawase += 2
        hikkuri -= 2
    elif hikkuri == 1:
        shiawase += 1
        break

print(shiawase)

Solution (Python):
l,k=map(int,input().split())
s=input()+'?'
ss=[]
prev=0
for i in range(l):
  if s[i]!=s[i+1]:
    ss.append(s[prev:i+1])
    prev=i+1
ll=len(ss)
s2=''
cnt=0
for i in range(ll):
  if i%2==0:
    s2+=ss[i]
  else:
    if cnt<k:
      cnt+=1
      if ss[i][0]=='R':
        s2+='L'*len(ss[i])
      else:
        s2+='R'*len(ss[i])
    else:
      s2+=ss[i]
s2+='?'
ans=0
cnt=0
for i in range(l):
  if s2[i]==s2[i+1]:
    cnt+=1
  else:
    ans+=cnt
    cnt=0
print(ans)

Solution (Python):
N, K = map(int, input().split())
S = input()
res = 0
c = 0
for i in range(N - 1):
    if S[i] == S[i + 1]:
        res += 1
    else:
        c += 1
        
if (c + 1)//2 <= K:
    print(res + c)
else:
    print(res + 2*K)

Solution (Python):
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 = (n-1) - max(cnt - k*2, 0)
print(ans)

Solution (Python):
import sys
import math

def input():
    return sys.stdin.readline()[:-1]

def main():
    N, K = map(int,input().split())
    S = "_" + input() + "_"
    if N == 1:
        print(0)
        exit(0)
    LR = 0
    ans = 0
    for k in range(1,N+1):
        if S[k] == S[k+1]:
            ans += 1
        else:
            LR += 1

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

if __name__ == '__main__':
    main()

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n, k = li()
s = ns()

if len(set(list(s))) == 1:
    print(n-1)
else:
    init = 0
    bef = ""
    for si in s:
        if si == bef:
            init += 1
        bef = si
    print(min(n-1, init + 2*k))

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

before = S[0]
div = 0

for s in S[0:]:
    if s != before:
        div += 1
    before = s

print(N-1-max(0, div-(2*K)))

Solution (Python):


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

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

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

Solution (Python):
n, k = map(int, input().split(" "))
s = input()
score = 0
for i in range(len(s) - 1):
    if s[i] == s[i+1]:
        score += 1

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

Solution (Python):
n,k=map(int,input().split())
hoge=list(input())
hap=0
for i,j in zip(hoge[0:],hoge[1:]):
  if i==j:hap+=1
print(min(hap+2*k,n-1))

Solution (Python):
N,K=list(map(int,input().split()))
S=list(input())
sum=0
for i in range(N-1):
    if S[i]==S[i+1]:
        sum+=1
sum+=2*K
if sum>=N:
    sum=N-1
print(sum)

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

count = len(S) - 1
mismatch = 0
for idx in range(len(S) - 1):
	if(S[idx] != S[idx+1]):
		mismatch += 1
mismatch = max(0, mismatch - 2 * K)
print(count - mismatch)

Solution (Python):
n,k=map(int,input().split())
s=input()

lr = 0
f=0

if s[0] == 'R':
  lr+=1
  f +=1
for i in range(n-1):
  if s[i] == 'L' and s[i+1] == 'R':
    lr += 1
if s[n-1]=='L':
  lr+=1
  f +=1

if k<lr-1:
  print(n-2*(lr-k)+f)
elif k==lr-1:
  print(n-1 if f>0 else n-2)
else:
  print(n-1)

Solution (Python):
n,k = list(map(lambda x:int(x),input().split()))
s = input()
if len(s) == 1:
    print(0)
    exit(0)
prev = ''
ans = ''
count = -1
flag = True
for i in range(len(s)):
    if s[i] != prev:
        count += 1
        prev = s[i]
        if count %2 == 1:
            k -= 1
            if k < 0:
                flag = False
                
    if count%2 == 1 and flag:
        if s[i] == 'L':
            ans += 'R'
        else:
            ans += 'L'
    else:
        ans += s[i]

count = 0
for i in range(1,len(ans)-1):
    if ans[i] == 'L' and ans[i-1] == 'L':
        count += 1
    elif ans[i] == 'R' and ans[i+1] == 'R':
        count += 1
if ans[0] == 'R' and ans[1] == 'R':
    count += 1
if ans[-1] == 'L' and ans[-2] == 'L':
    count += 1
print(count)

Solution (Python):

def main():
    N, K = [int(x) for x in input().split()]
    LR = input()

    count = 0
    for p in range(len(LR)-1):
        if LR[p] == LR[p+1]:
            count += 1
    
    count += K*2
    count = min(count, N-1)

    print(count)


if __name__ == "__main__":
    main()

Solution (Python):










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


count = 0

edge_count = 0


if S[0] == 'L':
    edge_count += 1
if S[N-1] == 'R':
    edge_count += 1 


for i in range(N-1):
    if S[i] == 'R':
        if S[i+1] == 'L':
            count += 1


happy = N  - (count*2 + edge_count)


if count >= K:
    print(min(N-1, K * 2 + happy  ))

else:
    print(min(N-1, K * 2 + happy + (min(edge_count,K - count) )))

Solution (Python):
n, k= map(int, input().split())
s=input()

unhappy_0=s.count('RL')*2+(s[0]=='L')+(s[-1]=='R') 
print(n-max(unhappy_0-k*2,1))

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

happy = 0

for i, s in enumerate(S):
    if s == "L":
        if i != 0 and S[i - 1] == "L":
            happy += 1
    else:
        if i != N - 1 and S[i + 1] == "R":
            happy += 1
print(min(N - 1, happy + 2 * K))

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

cnt1 = 0
cnt2 = 0

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

    
    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)))))

Solution (Python):
n, k = map(int, input().split())
s = input()

cnt = n - 1 - s.count('LR') - s.count('RL')
ans = min(n - 1, 2 * k + cnt)
print(ans)

Solution (Python):
N, K = map(int, input().split())
S = list(input())


S_pack = [S[0]]
for direction in S:
    if S_pack[-1] != direction:
        S_pack.append(direction)
n_unhappy = len(S_pack)
n_unhappy = max(n_unhappy - K*2, 1)

print(N - n_unhappy)

Solution (Python):




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


S_pack = [S[0]]
for direction in S:
    if S_pack[-1] != direction:
        S_pack.append(direction)

n_unhappy = len(S_pack)
n_unhappy = max(n_unhappy - K*2, 1)

print(N - n_unhappy)

Solution (Python):





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


S_pack = [S[0]]
for direction in S:
    if S_pack[-1] != direction:
        S_pack.append(direction)

n_unhappy = len(S_pack)

if n_unhappy%2 == 1:
    n_unhappy = max(n_unhappy - K*2, 1)
elif K >= n_unhappy/2:
    n_unhappy = 1
else:
    n_unhappy -= K*2

print(N - n_unhappy)