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 = input()



def count_happy(s):
    counter=False
    happy = 0
    for si in s:
        if si == "R" and counter:
            happy += 1
        elif si == "R":
            counter=True
        else:
            counter=False
    counter = False
    for si in s[::-1]:
        if si == "L" and counter:
            happy += 1
        elif si == "L":
            counter=True
        else:
            counter=False
    return happy


happy = count_happy(s)

new_s = [s[0]]
last_char = s[0]
for si in s[1:]:
    if si == last_char:
        continue
    else:
        new_s.append(si)
        last_char = si

happy += min(len(new_s)//2, k)*2

if len(new_s)%2 == 0 and len(new_s)//2 <= k:
    happy -= 1

print(happy)

Solution (Python):
"""
幸福な人(HP)でなく不幸な人(UHP)を数える。最終的にHPは HP = N - UHP で求めることができる。
以下のような並びを考える。

    L RR LLLL R L RRR L R LLLL

同じ記号ごとにスペースで区切っている。この中で不幸な人は、両端にいる2人と、スペースで
区切られたグループに付き1人である。このようにして不幸な人の人数を数えることができた。

また、(l, r) の人々を180度回転することによって、回転するグループ両端以外の幸福な人（不幸な人）
の人数は変化しない。両端の幸不幸が変化するのみである。

境界以外を l or r に選んだとしても、幸不幸の数は変化しない。
両端でない facing, unfacing な境界をそれぞれ l, r に選ぶと、
幸福な人を2人増やすことができる。

k が十分に大きい場合、十分な数の操作を行うことによって幸福な人を最大 N - 1 人に
することができる。
"""


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

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


hpp += 2 * k


print(min(n - 1, hpp))

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

against = [(S[i] != S[i+1]) for i in range(N-1)]
unhappy = sum(against)

if K > unhappy:
    print(N - 1)
else:
    print(N - max(unhappy - 2 * K + 1, 1))

Solution (Python):
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac
import bisect
import sys
import logging
import heapq
logging.basicConfig(level=logging.DEBUG)

input = sys.stdin.readline

setrecursionlimit(1000000)

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

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

main()

Solution (Python):
n, k = map(int, input().split())
s = input()
cnt = 1 + sum([s[i] != s[i + 1] for i in range(n - 1)])
print(n - max(1, cnt - 2 * k))

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

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 = [int(i) for i in input().split()]
s = input()

l_r = 0
r_l = 0
prev_s = s[0]
ans = 0
for cur_s in s[1:]:
    if prev_s == cur_s:
        ans += 1
    elif cur_s == 'L':
        r_l += 1
    else:
        l_r += 1
    prev_s = cur_s

if min(l_r, r_l) >= k:
    ans += 2 * k
else:
    ans += 2 * min(l_r, r_l)
    if max(l_r, r_l) >= k:
        ans += k - min(l_r, r_l)
    else:
        ans += max(l_r, r_l) - min(l_r, r_l)

print(ans)

Solution (Python):
n, k = map(int, input().split())
s = input()
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()
ans = 0
l, r = 0, 0
for i in range(N-1):
	if S[i] == S[i+1]:
		ans += 1
	elif S[i] == 'L':
		l += 1
	else:
		r += 1
x = min(l, r)
if K > x:
	print(ans + 2 * x + min(K - x, abs(l - r)))
else:
	print(ans + 2 * K)

Solution (Python):
N, K = map(int, input().split())
S = input()
pre = ""
cnt = 0
for c in S:
    if c != pre:
        pre = c
        cnt += 1

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

Solution (Python):
n, k = (int(i) for i in input().split())
s = input()
ans = 0
prev = ""
for c in s:
    if c == prev:
        ans += 1
    prev = c
ans = min(ans + k*2, n-1)
print(ans)

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

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


if len(count) - (2*K + 1) > 0:
    print(sum(count)-len(count)+2*K)
else:
    print(sum(count)-1)

Solution (Python):

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

answer = 0
preword = ''
for s in Smoji:
  if s == preword:
    answer += 1
  preword = s

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

print(answer)

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

happy_count = 0
for i, c in enumerate(s):
    if c == "R":
        if i == len(s) - 1:
            pass
        elif s[i + 1] == "R":
            happy_count += 1
    else:
        if i == 0:
            pass
        elif s[i - 1] == "L":
            happy_count += 1

ret = min(happy_count + 2 * k, len(s) - 1)
print(ret)

Solution (Python):

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

hap = 0
bef = ''
for s in S:
  if s == bef:
    hap += 1
  bef = s

hap = hap + K*2
if hap > N-1:
  hap = N-1

print(hap)

Solution (Python):

N,K=map(int,input().split())
S=input()
LRf=-1
RLf=-1
ans=0
for i in range(N-1):
  if S[i:i+2]=="LL" or S[i:i+2]=="RR":
    ans+=1

for i in range(N-1):
  if K==0:
    break
  if S[i:i+2]=="LR" and RLf==-1:
    LRf=i
  elif S[i:i+2]=="RL" and LRf>=0:
    LRf=-1
    ans+=2
    K-=1
  elif S[i:i+2]=="RL" and LRf==-1:
    RLf=i
  elif S[i:i+2]=="LR" and RLf>=0:
    RLf=-1
    ans+=2
    K-=1
  if i==N-2:
    if RLf>=0 or LRf>=0:
      ans +=1
      K-=1

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
 
import numpy as np
 
N, K = map(int, input().split())
S = np.array(list(input()), dtype='U1')
 
S_slide = S[1:]
S_2 = S[:-1]
 
happiness = np.count_nonzero(S_slide == S_2)
happiness += 2*K
 
happiness = min(happiness, N-1)
 
print(happiness)

Solution (Python):
import sys

sys.setrecursionlimit(5 * 10**5)
input=sys.stdin.readline

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

chunks = 0
between = 0

prev = S[0]

for s in S:
    if s != prev:
        if s == S[0]:
            between += 1
        chunks +=1
    prev = s


if K <= between:
    ans = N - chunks + 2 * K
else:
    ans = N - 1

print(ans)

Solution (Python):
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(min(cnt+K*2,N-1))

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

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

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

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

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
print(min([n-1,h+2*k]))

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

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

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

Solution (Python):
import sys
import math

def input():
    return sys.stdin.readline().strip()

def iinput():
    return int(input())

def finput():
    return float(input())

def tinput():
    return input().split()

def rinput():
    return map(int, tinput())

def rlinput():
    return list(rinput())

def pro(k, l, n):
    if l < n:
        if k == 0:
            return(n - l)
        return (n - l + 2)
    if l == n:
        if k == 0:
            return 0
        elif k == 1:
            return 1
        return 3
    else:
        l -= n - 1
        if l == k:
            return 1
        elif l > k:
            return 0
        if l < k - 1:
            return 2
        else:
            return 3
            

def rt(k, l , n):
    if k < n:
        if l == 0:
            return (k * 2 + n)
        return (n + k * 2 - 1)
    elif k == n:
        if l == 1:
            return (k * 3 - 2)
        elif l == 0:
            return (k * 3)
        return (k * 3 - 3)
    else:
        k -= n - 1
        if k == l:
            return (n * 3 - 2)
        elif k > l:
            return (n * 3)
        if k < l - 1:
            return ((n - 1) * 3 - 1)
        else:
            return ((n - 1) * 3)



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


main()

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

ans = 0
for a,b in zip(s,s[1:]):
    if a == b:
        ans += 1
ans += k*2
print(min(n-1,ans))

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


now = S[0]
cnt = 0
encode = []

for i in range(1, N):
    if now != S[i]:
        cnt += 1
    now = S[i]

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

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



ans = 0

u = 0
for i in range(1, n+1):
    if s[i] == 'L':
        if s[i-1] == 'L':
            ans += 1
    if s[i] == 'R':
        if s[i+1] == 'R':
            ans += 1
        else:
            u += 1

if k >= u:
    print(n-1)
else:
    print(ans + (k * 2))

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

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

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

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

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(n-1,cnt+k*2))

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

S = input()

if N==1:
    print(0)
    exit()

b = 0
p1 = 0
p2 = 0
for i in range(N):
    if i == 0:
        if S[i] == S[i+1]:
            b += 1
        if S[i] == "L":
            p1 += 1
        else:
            if S[i] == "R" and S[i+1] == "L":
                p2 += 1
    elif 0 < i < N-1:
        if S[i] == S[i+1]:
            b += 1
        if S[i] == "R" and S[i+1] == "L":
            p2 += 1
    elif i == N-1:
        if S[i] == "R":
            p1 += 1


if K <= p2:
    b += 2*K
else:
    b += 2*K
    K -= p2
    b += max(p1, K)

print(min(N-1,b))

Solution (Python):
from sys import stdin
N, K = [int(_) for _ in stdin.readline().rstrip().split()]
S = stdin.readline().rstrip()
count = 0
for i in range(N-1):
    if S[i] == S[i+1]:
        count += 1
print(min(N-1, count+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
happy=n-cnt-1
print(min(n-1,happy+2*k))

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

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

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

sc = 0
lr = 0
rl = 0

for i in range(n-1):
    c = s[i]
    nc = s[i+1]

    if c == nc:
        sc += 1
    elif c == 'L' and nc == 'R':
        lr += 1
    elif c == 'R' and nc == 'L':
        rl += 1

if rl == lr:
    dc = lr
    oc = 0
else:
    dc = min(lr, rl)
    oc = 1

if dc + oc <= k:
    print(dc*2 + oc + sc)
else:
    print(sc + k*2)

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

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

s_c = []

left = 0
right = 0
for i in range(n - 1):
    if s[i] == s[i + 1]:
        right = i + 1
    else:
        s_c.append(''.join(s[left:right + 1]))
        i += 1
        left = i
        right = i
s_c.append(''.join(s[left:right + 1]))

s_c_len = len(s_c)
cc = 'R'
if s_c_len > 1 and s_c[1][0] == 'R':
    cc = 'L'

for i in range(k):
    if (2 * i + 1) > s_c_len - 1:
        break
    s_c[2 * i + 1] = cc * len(s_c[2 * i + 1])

ans_s = ''.join(s_c)
ans = 0
for i in range(n):
    if ans_s[i] == 'R' and i != n - 1 and ans_s[i + 1] == 'R':
        ans += 1
    elif ans_s[i] == 'L' and i != 0 and ans_s[i - 1] == 'L':
        ans += 1
print(ans)

Solution (Python):
n,s=open(0);n,k=map(int,n.split());print(min(n-1,k*2+sum(i==j for i,j in zip(s,s[1:]))))

Solution (Python):
n,s=open(0);n,k=map(int,n.split());print(min(n-1,k*2+sum(i==j for i,j in zip(s,s[1:]))))

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

sc = 0
lr = 0
rl = 0

for i in range(n-1):
    c = s[i]
    nc = s[i+1]

    if c == nc:
        sc += 1
    elif c == 'L' and nc == 'R':
        lr += 1
    elif c == 'R' and nc == 'L':
        rl += 1

if rl > 1 or lr > 1:
    dc = min(lr, rl)
    oc = abs(lr-rl)
elif rl == 1 or lr == 1:
    dc = 0
    oc = 1
else:
    dc = 0
    oc = 0

dnum = min(k, dc)
onum = 0
if k - dc > 0 and oc == 1:
    onum += 1
print(onum + sc + dnum*2)

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

Solution (Python):
def solve(n, k, s):
    s = list(map(lambda c: c=='L', s))
    i = 1
    while i < n:
        if (k > 0) and (s[i] != s[i-1]):
            l = i
            while (i+1 < n) and (s[i] == s[i+1]):
                i += 1
            r = i
            for j in range(l, r+1):
                s[j] = not s[j]
            k -= 1
        i += 1
    ans = 0
    for i in range(1, n):
        if s[i] == s[i-1]:
            ans += 1
    return ans

n, k = map(int, input().split())
s = input()
print(solve(n, k, s))

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

Solution (Python):

import sys
sys.setrecursionlimit(300000)


def solve(N: int, K: int, S: str):
    ret = 0
    cnt = 1
    tmp = 0
    for i in range(N - 1):
        if S[i] == S[i + 1]:
            cnt += 1
        else:
            if cnt > 1:
                ret += cnt - 1
            cnt = 1
            tmp += 1
    ret += cnt - 1
    ret += min(K, tmp // 2) * 2
    if K > tmp // 2:
        ret += tmp % 2
    ret = min(ret, N - 1)
    print(ret)
    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):
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)

Solution (Python):



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

c = c + 2*k
print(c) if c < n-1 else print(n-1)

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

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

ans=c+2*K

if ans<=N-1:
  print(ans)
else:
  print(N-1)

Solution (Python):
n = input().split()
N,K = int(n[0]),int(n[1])
S = list(input())

count = 0
count_s1 = 0
count_sn = 0
s_1 = S[0]
if s_1 == "L":
    count_s1 += 1
s_N = S[-1]
if s_N == "R":
    count_sn += 1

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

if K <= count:
    print(N - ((count-K)*2 + count_s1 + count_sn))
else:
    print(N-1)

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

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

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

Solution (Python):
n, k = map(int, input().split())
s = list(input())
r = []
num = 1


for i in range(0, n-1):
  if s[i] == s[i+1]:
    num += 1
  else:
    r.append(num)
    num = 1
r.append(num)


for j in range(k):
  if len(r) == 2:
    r[0] = r[0] + r[1]
    r.pop(1)
  elif len(r) == 1:
    pass
  else:
    r[0] = r[0] + r[1] + r[2]
    r.pop(2)
    r.pop(1)

ans = sum(r) - len(r)
print(ans)

Solution (Python):
N, K = map(int, input().split())
S = str(input())
cnt = 0
l = 0
r = 0

for i in range(N-1):
    if S[i] == "R" and S[i+1] == "L":
        cnt += 1
if S[0] == "L":
    l += 1
if S[N-1] == "R":
    r += 1
    
unhappy = 0
if cnt + max(l,r) <= K:
    unhappy = 1
else:
    unhappy = 2 * (cnt - K) + l + r
print(N - unhappy)

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

ans = 0
tmp_s = None
for s in S:
    if tmp_s == s:
        ans+=1
    tmp_s = s

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

print(ans)

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

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

before = s[0]
cnt=0

for current in s:
    if current != before:
        cnt += 1
    before = current

score_max = len(s)-1
print(min(score_max, score_max-cnt+k*2))

Solution (Python):
n,k = map(int,input().split())
s = [1 if ele=="L" else 0 for ele in input()]

before = s[0]
cnt=0

for si in s:
    if si != before:
        cnt += 1
    before = si

score_max = len(s)-1
print(min(score_max, score_max-cnt+k*2))

Solution (Python):
n,k = map(int,input().split())
s = [1 if ele=="L" else 0 for ele in input()]

if s[0]==0:
    s=[(1-ele) for ele in s]

before = s[0]
cnt=0
for si in s:
    if si != before:
        cnt += 1
    before = si

score_max = len(s)-1
print(min(score_max, score_max-cnt+k*2))

Solution (Python):














import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys


def solve(N, K, S):
    prev = S[0]
    change = 0
    for ch in S[1:]:
        change += ch != prev
        prev = ch

    happiness = N - 1 - change

    
    use = min(K, change // 2)
    happiness += use * 2
    K -= use

    
    if K > 0 and happiness != N - 1:
        happiness += 1

    return happiness


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

Solution (Python):
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
S = input()
change_count = 0
first = S[0]
for i in range(1,N):
    if first != S[i]:
        change_count += 1
        first = S[i]
print(N - 1 - max(0,change_count-K*2))

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

for i in range(1, len(s)):
    if s[i-1] == s[i]:
        h += 1
        
print(min(n-1, h+k*2))

Solution (Python):
def main():

    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((ans + 2 * K), N - 1))

if __name__ == "__main__":
    main()

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

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,K = MI()
    S = ST()
    before = "
    RLE = []
    for i,s in enumerate(S):
        if s != before:
            before = s
            RLE.append(1)                        
        else:
            RLE[-1]+=1
    ans = sum(RLE) - len(RLE) 
    cnt = len(RLE)
    for i in range(K):
        if cnt >= 3:
            ans += 2
        elif  cnt == 2:
            ans += 1
        cnt -=1
        if not cnt:
            break
    print(min(ans,N-1))
        
if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
from collections import deque
def getN():
    return int(input())
def getList():
    return list(map(int, input().split()))
import math

n, k = getList()

s = input().strip()
s = s

state = "X"
cur_seq = 0
seq_nums = []
n_separate = 0


for c in s:
    
    if state == c:
        cur_seq += 1
    else:
        state = c
        seq_nums.append(cur_seq)
        cur_seq = 1
        n_separate += 1



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

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

n_flip = 0
s_last = s[0]
for i in range(1, len(s)):
    if s[i] != s_last:
        n_flip += 1
    s_last = s[i]
    if n_flip % 2 == 1 and n_flip // 2 < k:
        s[i] = s[0]

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

print(n_happy)

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

last = ''
count = 0
face_count = 0
for i in range(len(s)):
    if i == 0: pass
    else:
        if s[i] == last:
            count += 1
        else:
            face_count += 0.5
    last = s[i]

if face_count > k:
    print(count + 2*k)
elif face_count <= k:
    print(n - 1)

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

Solution (Python):
n, k = map(int, input().split())
s = input()
happy = 0
try1 = 0
for i in range(n - 1):
    if s[i] == s[i + 1]:
        happy += 1
while happy <= n - 2 and try1 < k:
    if happy <= n - 3:
        happy += 2
    else:
        happy += 1
    try1 += 1
print(int(happy))

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

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

Solution (Python):
def solve(string):
    n, k, s = string.split()
    c = s.count("RL") + s.count("LR")
    return str(int(n) - max(1, c + 1 - 2 * int(k)))


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Solution (Python):
def solve(string):
    n, k, s = string.split()
    n, k = map(int, [n, k])
    ans = n - 1 - s.count("RL") - s.count("LR")
    return str(min(ans + 2 * k, n - 1))


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Solution (Python):
def solve(string):
    n, k, s = string.split()
    k = int(k)
    ans = change = 0
    for s0, s1 in zip(s, s[1:]):
        if s0 == s1:
            ans += 1
        else:
            change += 1
    return str(ans + min(2 * k, change))


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Solution (Python):
















import sys
input = sys.stdin.readline

n, k = list(map(int, input().split()))
l = input().strip()
ans = 0
for i in range(n - 1):
	if l[i] == l[i+1]:
		ans += 1
print(min(ans + 2 * k, n - 1))

Solution (Python):
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 * 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*count -1)
        else:
            print(count2 + 2*K)

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

Solution (Python):
import re
from collections import deque

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

token = re.findall(r'(L+|R+)', S)

def flip(s):
    if s[0] == 'R':
        return 'L'*len(s)
    else:
        return 'R'*len(s)
    
even = False
flipped_token = deque([])

flip_count = 0
for s in token:
    if flip_count >= K:
        flipped_token.append(s)
    else:
        if even:
            s = flip(s)
            flip_count += 1
            flipped_token.append(s)
            even = False
        else:
            flipped_token.append(s)
            even = True

flipped_S = ''.join(flipped_token)
        
token = re.findall(r'(L+|R+)', flipped_S)

happy_count = 0

for s in token:
    l = len(s)
    happy_count += l-1

print(happy_count)

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

s_comp = []
cnt = 1


for i in range(n):
    
    if i == n-1:
        if s[i-1] != s[i]:
            cnt = 1
            s_comp.append(str(s[i])*cnt)   
        else:
            s_comp.append(str(s[i])*cnt)  
    
    elif s[i] == s[i+1]:
        cnt+=1
    
    else:
        s_comp.append(str(s[i])*cnt)
        cnt = 1


out = 0
cnt = 0

for i in range(1,len(s_comp),2):
    cnt+=1
    
    if cnt > k:
        break
    if s_comp[i][0] == "R":
        s_comp[i] = "L"*len(s_comp[i])
    elif s_comp[i][0] == "L":
        s_comp[i] = "R"*len(s_comp[i])
    


s = ""
for i in range(len(s_comp)):
    s += str(s_comp[i])


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

print(cnt)

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

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

now = S[0]
change = 1
for i in range(1, len(S)):
    if now != S[i]:
        change += 1
    now = S[i]

if change == 1 or change == 2 or (change-2) <= K:
    print(N - 1)
else:
    print(min(N - 1, N - change + 2 * K))

Solution (Python):
"""別解
解説動画でやってること。

考察問題。

ランレングス法みたいな考え方をする。

LグループとRグループに分けて考える。

LグループとRグループの境目を選択して操作をすると、基本的に幸福な人の数を＋２できる

ただし、端っこの境目を選んだときのみ、＋１になる。


・入力例２を考える


 L R R L R L R R L R L L R
   .         .         .     幸福な人：3人




 L R R L R L R R L R L L R
  |___|

 L L L L R L R R L R L L R
   . . .     .         .     幸福な人：5人



 L L L L R L R R L R L L R
        |_|

 L L L L L L R R L R L L R
   . . . . . .         .     幸福な人：7人



 L L L L L L R R L R L L R
            |___|

 L L L L L L L L L R L L R
   . . . . . . . .     .     幸福な人：9人



 L L L L L L L L L R L L R
                  |_|

 L L L L L L L L L L L L R
   . . . . . . . . . . .     幸福な人：11人



 L L L L L L L L L L L L R
                        |_|

 L L L L L L L L L L L L L
   . . . . . . . . . . . .   幸福な人：12人


この考察からすると、初期状態の幸福な人の数を score とすると、

・K回操作して、初期状態の幸福な人に加えて 2*K 人を幸福にすることができたパターン = score + 2*K
・K回以下の操作で、すべての人を L(R) にすることができたパターン = N-1

のどちらかなので、最終的な幸福な人の数 ans は、

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

となる。

初期状態の幸福な人の数 score をO(N)で数えればよいだけなので、計算量はO(N)

"""
def solve2():
    N, K = list(map(int, input().split()))
    S = input()

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

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


if __name__ == "__main__":
    solve2()