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

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

happy += 2*k
if happy >= n:
    happy = n-1

print(happy)

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
order=0
left=0
if cnt%2==0:
    order=cnt//2
elif cnt%2!=0:
    order=cnt//2
    left=1
checker=s[0]
count=0
lists=[]

s=s+"e"
for i in range(n+1):
    if s[i]==checker:
        count+=1
    elif s[i]!=checker:
        lists.append(count-1)
        count=1
        checker=s[i]
base=sum(lists)

if order>=k:
    ans=k*2+base
elif order<k:
    ans=n-1
print(ans)

Solution (Python):
n,k = map(int,input().split())
s = input()
key = s[0]
count = 0
for i in range(1,n):
    if key == s[i]:
        count += 1
    else:
        key = s[i]

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

Solution (Python):




def main():
  _, k = map(int, input().split())
  vs = [int(v == 'L') for v in input().strip()]
  h = sum(int(a == b) for a, b in zip(vs[:-1], vs[1:]))
  print(min(h + 2 * k, len(vs) - 1))


if __name__ == '__main__':
  main()

Solution (Python):
N ,K = map(int, input().split())
S = input()
kirikae = 0
kirikae2 = 0
flag = 0
for i in range(N-1):
    if S[i] != S[i+1]:
        kirikae += 1
kirikae = kirikae - (K * 2)
if kirikae <= 0 :
    kirikae = 0

print(N - 1 - kirikae)

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

if __name__ == '__main__':
    solve()

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

num_group = 1
for p1, p2 in zip(s, s[1:]):
    if p1 != p2:
        num_group += 1

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

print(score)

Solution (Python):
from math import ceil
N, K = map(int, input().split())
S = 'x' + input() + 'x'

ans = 0
X = ''
ns = ''

for i, s in enumerate(S[1:-1], start=1):
    if s == 'L':
        if S[i - 1] == 'L':
            ans += 1
        else:
            X += s

    else:
        if S[i + 1] == 'R':
            ans += 1
        else:
            X += s


target = 'L' if X[0] == 'R' else 'R'
k_cnt = 0
for i, x in enumerate(X):
    if x == target and k_cnt < K:
        k_cnt += 1
        if i == len(X) - 1:
            ans += 1
        else:
            ans += 2

print(ans)

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

Solution (Python):
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7

n,k = MAP()
s = input()
unhappy=0
if s[0]=="L":
    unhappy+=1
if s[n-1]=="R":
    unhappy+=1

for i in range(n-1):
    if s[i:i+2]=="RL":
        unhappy+=2
print(n-max(1,unhappy-(2*k)))

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

ans = 0

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(pow(10, 6))


def main():
    n, k = map(int, input().split())
    s = input().strip()
    prevs = s[0]
    la = [0]
    for _s in s:
        if prevs == _s:
            la[-1] += 1
        else:
            la.append(1)
            prevs = _s
    
    ans = 0
    is_swap = False
    for i in range(0, len(la), 2):
        ans += la[i]-1
        if is_swap:
            ans += 1
            is_swap = False
        if i+1 < len(la) and k >= 1:
            ans += la[i+1]
            k -= 1
            is_swap = True
        elif i+1 < len(la):
            ans += la[i+1]-1
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

import sys
import math
import numpy as np


args = sys.argv
if len(args) == 2:
  from IPython import embed
  em = True
else:
  em = False


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






if em: sys.stdin = open('/dev/tty')







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

num_R = len([s for s in S if s == "R"])

if num_R > len(S) / 2:
  S2 = S.split("R")
else:
  S2 = S.split("L")

edge     = len([s for s in [S2[0],S2[-1]] if s != ""])
non_edge = len([s for s in S2[1:-1] if s != ""])



score = score + 2 * min(K, non_edge)
K = K - min(K, non_edge)
score = score + min(K, edge)

print(score)

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

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

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

for i in range(len(S)):
  if S[i] == "L":
    S[i] = 0
  else:
    S[i] = 1

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

ans = count + 2 * K
if ans > len(S) - 1:
  ans = len(S) - 1
print(ans)

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

if s[0] == 'R':
    left_happy = 1
else:
    left_happy = 0
if s[-1] == 'L':
    right_happy = 1
else:
    right_happy = 0
edge_happy = left_happy + right_happy

renzoku = 0
original_happy = 0
before = 0
unhappy = 0
for i in range(n):
    now = s[i]
    if before == now:
        renzoku += 1
    else:
        original_happy += renzoku
        renzoku = 0
    if before == 'R' and now == 'L':
        unhappy += 1
    before = now
original_happy += renzoku

if edge_happy == 0:
    if k <= unhappy:
        print(original_happy + k*2)
    else:
        print(original_happy + unhappy*2 + 1)
elif edge_happy == 1:
    if k <= unhappy:
        print(original_happy + k*2)
    else:
        print(original_happy + unhappy*2)
else:
    if k < unhappy:
        print(original_happy + k*2)
    else:
        print(original_happy + unhappy*2 - 1)

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

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

def g(t):
    l = []
    b = "x"
    c = 0
    for i, t_i in enumerate(t):
        if b != t_i:
            l.append(c)
            c = 0
        c += 1
        b = t_i
    l.append(c)
    return l[1:]

l = g(S)
len_l = len(l)
score = sum(l)-len_l

score += min(len_l//2, K)*2 - (len_l%2==0 and len_l//2 <= K)

print(score)

Solution (Python):

N, K = input().strip().split()
N, K = [int(N), int(K)]
S = [None] + list(input().strip())



num_dirchange = 0  
num_happy = 0
for i in range(1, N+1): 
    
    if i > 1 and S[i] is not S[i-1]:
        num_dirchange += 1
    
    if S[i] is 'L':
        if i > 1 and S[i-1] is 'L':
            num_happy += 1
    if S[i] is 'R':
        if i < N and S[i+1] is 'R':
            num_happy += 1




if num_dirchange >= 2 * K:
    num_happy = num_happy + 2*K
else:
    num_happy = N - 1  

print(num_happy)

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

h = 0
lr = 0
rl = 0
for i in range(1,N):
  if S[i-1] == S[i] :
    h += 1
  elif S[i-1] == 'L' and S[i] == 'R':
    lr += 1
  else:
    rl += 1


while (K>0 and (lr>0 or rl>0)):
  if lr > 0:
    lr -= 1
    h += 1
  if rl > 0:
    rl -= 1
    h += 1
  K -= 1
print(h)

Solution (Python):
if __name__=='__main__':
    a = [int(i) for i in input().split(' ')]
    n = a[0]
    k = a[1]
    s = input()
    cnt = 0
    ans = 0
    for i in range(len(s)-1):
        if s[i] != s[i+1]:
            cnt += 1
        else:
            ans += 1
    if cnt > 2*k:
        ans += k*2
    if cnt <= 2*k:
        ans = len(s)-1
    print(ans)

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

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


if __name__ == "__main__":
    main()

Solution (Python):

import sys


def solve(N: int, K: int, S: str):
    Y = 0
    X = 0

    if S[0] == "L":
        Y += 1
    if S[-1] == "R":
        Y += 1

    for i in range(len(S)-1):
        if S[i:i+2] == "RL":
            X += 1

    H = len(S) - 2 * X - Y
    H += min(K,X) * 2
    if K-X > 0:
        H += min(K-X, Y) 

    H = min(len(S)-1, H)

    print(H)




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 = input()
t = 0
for i in range(n - 1):
    if s[i] == s[i + 1]:
        t += 1
print(min([n - 1, t + 2 * k]))

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

Solution (Python):


import sys,collections,itertools,re,math,fractions,decimal,random,array,bisect,heapq





def solve(f):
    n, k = f.read_int_list()
    s = f.read_str()

    diff = 0
    for i in xrange(n-1):
        if s[i] != s[i+1]:
            diff += 1

    return n-1-max(0, diff-2*k)


class Reader(object):
    def __init__(self, filename=None):
        self.file = open(filename) if filename is not None else None
        self.case = 1

    def __readline(self):
        return self.file.next().strip() if self.file else raw_input()

    def next_case(self):
        self.file.next()
        self.case += 1

    def read_int(self): return int(self.__readline())
    def read_float(self): return float(self.__readline())
    def read_long(self): return long(self.__readline())
    def read_decimal(self): return decimal.Decimal(self.__readline())
    def read_str(self): return self.__readline()

    def read_int_list(self): return map(int, self.__readline().split())
    def read_float_list(self): return map(float, self.__readline().split())
    def read_long_list(self): return map(long, self.__readline().split())
    def read_decimal_list(self): return map(decimal.Decimal, self.__readline().split())
    def read_str_list(self): return self.__readline().split()

if __name__ == '__main__':
    filename = sys.argv[1] if len(sys.argv) > 1 else None
    f = Reader(filename)
    if f.file:
        while True:
            print "Case 
            try:
                f.next_case()
            except StopIteration:
                break
    else:
        print solve(f)

Solution (Python):









N,K = map(int,input().split())
S = input()
LL = 0
RR = 0
LR = 0
RL = 0
for i in range(N-1):
    if S[i]=="L" and S[i+1]=="L":
        LL += 1
    if S[i]=="R" and S[i+1]=="R":
        RR += 1
    if S[i]=="L" and S[i+1]=="R":
        LR += 1
    if S[i]=="R" and S[i+1]=="L":
        RL += 1
ans = 0
Kaisu = min(LR,RL,K)
ans += LL+RR+Kaisu*2

if abs(LR-RL)==1 and K-min(RL,LR)>=1:
    ans += 1
elif abs(LR-RL)>1 and K-min(RL,LR)==1:
    ans += 1
elif abs(LR-RL)>1 and K-min(RL,LR)>1:
    ans += 2

print(ans)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    n = II()
    print(n**3)
    return


def B():
    n = II()
    a = LI()
    b = LI()
    c = LI()
    ans = 0
    for i in range(n):
        ans += b[a[i] - 1] + (c[a[i] - 1] if i != n - 1 and a[i] + 1 == a[i + 1] else 0)
    print(ans)
    return


def C():
    n = II()
    b = LI()
    ans = b[0]
    for i in range(n-1):
        if b[i] >= b[i + 1]:
            ans += b[i + 1]
        else:
            ans += b[i]
    print(ans)
    return


def D():
    n, k = LI()
    s = S()
    a = []
    for i in range(n):
        if i == n - 1:
            a.append(s[i])
        elif s[i] != s[i + 1]:
            a.append(s[i])
    print(min(n - 1, n - len(a) + 2 * k))
    return


def E():
    return


def F():
    return



if __name__ == '__main__':
    D()

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

countR = S.count("R")
countL = S.count("L")
idx_list = []

for i in range(1, N):
    if S[i-1] != S[i]:
        idx_list.append(i)
idx_list.append(N)

i = 1
count = 0
for i in range(1, len(idx_list), 2):
    idx_start = idx_list[i - 1]
    idx_end = idx_list[i]
    
    for j in range(idx_start, idx_end):
        if S[j] == "R":
            S[j] = "L"
        else:
            S[j] = "R"    
    
    count += 1
    if count == K:
        break

ans = 0
for i in range(len(S)):
    if i == 0 and S[i] == "L":
        continue
    if i == (len(S)-1) and S[i] == "R":
        break
    
    if (S[i] == "L" and S[i-1] == "L") or (S[i] == "R" and S[i+1] == "R"):
        ans += 1

print(ans)

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

Solution (Python):
N,K=[int(s) for s in input().split()]
S=list(input())
a=S[0]
index=1
for i in range(K):
  while (index<N and S[index]==a) :
    index+=1
  while (index<N and S[index]!=a) :
    S[index]=a
    index+=1


count=0
for i in range(1,N-1):
  if S[i]=="L":
    if S[i-1]=="L":
      count+=1
  if S[i]=="R":
    if S[i+1]=="R":
      count+=1
if (S[0]=="R"and S[1]=="R"):
  count+=1
if (S[N-1]=="L"and S[N-2]=="L"):
  count+=1
if N>1:
  print(count)
else:
  print(0)

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

h = N
last = ''
for s in S:
  if s != last:
    h -= 1
  last = s
print(min(N-1, h+2*K))

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

count = 0

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

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

print(sum_happiness)

Solution (Python):
N,K = map(int,input().split())
S = input()
happiness = N-1 - S.count('LR') - S.count('RL')
print(min(N-1,happiness+2*K))

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

same = 0

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

dif = n - 1 - same

print(same + min(k * 2, dif))

Solution (Python):


n, k = map(int, input().split())
str_n = input()
first = str_n[0]
last = ""
if first == "R":
    last = "L"
else:
    last = "R"

for count in range(k): 
    new_line = str_n
    next_last = new_line.find(last)
    next_first = new_line.find(first, next_last)
    next_line = new_line[0:next_last]
    if next_first != -1:
        next_line += new_line[next_last:next_first].replace(last, first) + new_line[next_first:]
    else:
        next_line += new_line[next_last:].replace(last, first)
    str_n = next_line

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

print(count)

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

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

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

c = 0
for i in range(n):
    if s[i] == 'R' and 0 <= i + 1 < n and s[i+1] == 'R': c += 1
    if s[i] == 'L' and 0 <= i - 1 < n and s[i-1] == 'L': c += 1
ans = min(n - 1, c + 2 * k)
print(ans)

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

count = 0

for i in range(n):
    if s[i:i+2] == 'LL' or s[i:i+2] == 'RR':
        count += 1
    

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

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

def check_happiness(people):
    num_happiness = 0
    for i in range(0, len(people)-1):
        if people[i] == 'R' and people[i+1] == 'R':
            num_happiness += 1
        if people[i] == 'L' and people[i+1] == 'L':
            num_happiness += 1
    return num_happiness

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

Solution (Python):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7



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

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

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

Solution (Python):
n, k = map(int, input().split())
s = input()
happy_counter = 0
for count in range(1, n):
    if s[count] == s[count - 1]:
        happy_counter += 1
happy_counter += k * 2
if happy_counter > n - 1:
    happy_counter = n - 1

print(happy_counter)

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

ans = 0
S = "x"+"".join(S)+"x"
for i in range(len(S)):
    if (S[i-1]=="L" and S[i]=="L") or (S[i]=="R" and S[i+1]=="R"): ans += 1
print(min(ans+K*2, N-1))

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

Solution (Python):
N, K = list(map(int,input().split()))
S = str(input())
num_L = 0
num_R = 0
tmp = ""
count = 0
for i in range(len(S)):
    if S[i] == "L":
        if tmp == "L":
            count += 1
        else:
            num_L += 1
    else:
        if tmp == "R":
            count += 1
        else:
            num_R += 1
    tmp = S[i]
if K >= min(num_L, num_R):
    ans = N - 1
else:
    ans = count + 2*K
print(ans)

Solution (Python):


import sys
input = sys.stdin.readline
N, K = map(int, input().split())
S = input()

prev_c = None
score = 0
for c in S:
    if c == prev_c:
        score += 1
    prev_c = c

score += 2 * K

print(min((N - 1, score)))

Solution (Python):



N, K = map(int, input().split())
S = str(input())
Scount = []
ans = int(0)
count= int(0)
max = int(0)

count +=1
for i in range(1,N):

    if S[i]==S[i-1]:
        count +=1
        if i==N-1:
            Scount.append(count)
    else:
        Scount.append(count)
        count = int(1)


for i in Scount:

    max += i
    max -= 1

max += K*2

ans = min(max,N-1)

print(ans)

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

c = 0
for i in range(n):
    if s[i] == 'L' and i > 0 and s[i-1] == 'L':
        c += 1
    if s[i] == 'R' and i < n-1 and s[i+1] == 'R':
        c += 1
print(min(c + 2* k, n-1))

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

ans=0
for i in range(n-1):
    if s[i]==s[i+1]:
        ans+=1
count=0        
c='Q'
for i in range(n):
    if s[i]!=c:
        count+=1
        c=s[i]
    else:
        continue
if count==2:
    print(n-1)
elif count%2==0:
    count=count//2
    print(int(ans+min(2*count-1,2*k)))

elif count%2!=0:
    count=count//2
    print(int(ans+min(2*count,2*k)))

Solution (Python):
N,K = map(int,input().split())
S = list(input())
siawase = 0
 
LRchange_count = -1
LRittiFlg = True
temp_char = 'temp'

 
for s in S:
    
    if s != temp_char:
        LRchange_count += 1
    else:
        siawase += 1
    temp_char = s
    
if S[0] == S[-1]:
    LRittiFlg = True
else:
    LRittiFlg = False
    
if K+1 >= LRchange_count:
    print(len(S)-1)





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

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

S = []
score = 0
for i in range(len(s)-1):
  if i == 0:
    _s = s[i]
  else:
    _s += s[i]
  if s[i] != s[i+1]:
    S.append(_s)
    _s = ''
  else:
    score += 1

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

print(ans)

Solution (Python):
n, k = map(int, input().split())
s = list(input())
group_num = 1
tmp_char = s[0]
for index in range(1, len(s)):
    if s[index] == tmp_char:
        continue
    else:
        tmp_char = s[index]
        group_num += 1
group_num = max(1, group_num - (k-1)*2)
if group_num >= 3:
    group_num -= 2
elif group_num == 2:
    group_num = 1

happy = len(s) - group_num
print(happy)

Solution (Python):
import sys
N, K = map(int, input().split())
S = input()
S0 = S[0]
SL = S[-1]
if N == 1:
    print(0)
    sys.exit(0)
else:
    cnt = S.count('RL')
    answer = 0
    if S0 == 'L' and SL == 'L':
        answer = N - 1
        answer -= cnt*2
        answer += K*2
        answer = answer if answer < N-1 else N-1
    if S0 == 'L' and SL == 'R':
        answer = N - 1
        answer -= cnt*2+1
        answer += K*2
        answer = answer if answer < N-1 else N-1
    if S0 == 'R' and SL == 'L':
        answer = N - 1
        answer -= cnt*2-1
        answer += K*2
        answer = answer if answer < N-1 else N-1
    if S0 == 'R' and SL == 'R':
        answer = N - 1
        answer -= cnt*2
        answer += K*2
        answer = answer if answer < N-1 else N-1
        
    print(answer)

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

Solution (Python):


import math
import os
import sys


DEBUG = 'DEBUG' in os.environ


def inp():
    return sys.stdin.readline().rstrip()


def dprint(*value, sep=' ', end='\n'):
    if DEBUG:
        print(*value, sep=sep, end=end)


def solve(N, K, S):
    A = []
    i = 0
    while i < N:
        j = i
        while j < N and S[j] == S[i]:
            j += 1
        A.append(j - i)
        i = j

    assert sum(A) == N
    cur = N - len(A)
    return min(cur + 2 * K, N - 1)


def main():
    N, K = [int(e) for e in inp().split()]
    S = inp()
    print(solve(N, K, S))


if __name__ == '__main__':
    main()

Solution (Python):
n, k = map(int, input().split())
s = input()
cnt = s.count('LR') + s.count('RL') + 1
ans = n - cnt + (2 * min(k, cnt))
print(min(n-1, ans))

Solution (Python):
def main():
    n, k = map(int, input().split())
    s = input()
    a = s.count("RL")  
    ans = 0  
    
    for i in range(n - 1):
        if s[i] == s[i + 1]:
            ans += 1  
    for i in range(1, k + 1):
        
        
        
        if n - 1 - ans > 1:
            ans += 2
        else:
            ans += n - 1 - ans
            break
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N, K = map(int, input().split())
S = input()
A = ["N"]*N
base = S[0]
cnt = 0
comp = False
i=0
while i < N:
    if not comp and S[i]!=base:
        if cnt < K:
            comp = True
            cnt+=1
    if comp and S[i]==base:
        comp = False
    if not comp:
        A[i] = S[i]
    else:
        A[i] = base
    i+=1

ans = 0
for i in range(1,N):
    ans += A[i-1] == A[i]
print(ans)

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

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

if lr==rl:
    if k<rl:
        ans+=(2*k)
    else:
        ans+=(2*rl)
else:
    if rl>lr:
        if k<rl:
            ans+=(2*k)
        else:
            ans+=((2*lr)+1)
    else:
        if k<lr:
            ans+=(2*k)
        else:
            ans+=((2*rl)+1)


print(ans)

Solution (Python):

from itertools import *
import sys
import time










def read_input():
	[N, K] = map(int, raw_input().split())
	S = raw_input().rstrip()
	return K, S





K, S = read_input()
v = [ sum(1 for _ in w) for k, w in groupby(S) ]
n = v[0] - 1
for k in range(K):
	if len(v)/2 == k:
		break
	n += v[k*2+1]
	if k*2+2 < len(v):
		n += v[k*2+2]
n += sum(m-1 for m in v[K*2+1:])
print(n)

Solution (Python):


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

count_RL, count_LR = S.count("RL"), S.count("LR")


if max(count_LR, count_RL) <= K:
    print(N-1)
elif max(count_LR, count_RL) > K:
    if count_RL == count_LR:
        happy = N-1-2*(count_LR-K)
    elif not count_RL == count_LR:
        happy = N-1-2*(min(count_LR, count_RL)-K)-1
    print(happy)

Solution (Python):
import math
N, K = map(int, input().split())
S = list(input())
ans = [0]*(N-1)

for i in range(N-1):
    if S[i] != S[i+1]:
        ans[i] = 1
total = sum(ans)
if K >= math.ceil(total/2):
    print(N-1)
else:
    print(N-1-(total-2*K))

Solution (Python):

a,b = map(int,input().split())
l = list(input())
s = 0 
for n,(x,y) in enumerate(zip(l,l[1:])):
    if x == y:s+=1
print(min(a-1,s+b*2))

Solution (Python):
t = list(map(int, input().split())); n, k = t[0], t[1];
stripe = str(input()); groups = 0
for i in range(len(stripe)-1):
    if stripe[i] != stripe[i+1]:
        groups += 1
groups += 1
if k >= groups//2:
    print(n-1)
else:
    groups = groups - 2 * k
    print(n - groups)

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

cnt = 1

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

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

Solution (Python):
n, k = map(int, input().split())
s = input()
a = s.count("RL")
ans = 0
for i in range(n - 1):
    if s[i] == s[i + 1]:
        ans += 1
for i in range(1, k + 1):
    if n - 1 - ans > 1:
        ans += 2
    else:
        ans += n - 1 - ans
        break
print(ans)

Solution (Python):
def main():
    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(count+2*k,n-1))  


if __name__ == "__main__":
    main()

Solution (Python):
N, K = map(int, input().split())
S = input()
l = 0
T = []
while l < N:
    ch = S[l]
    r = l+1
    while r < N:
        if S[r] == ch:
            r += 1
        else:
            break
    T.append(ch)
    l = r

print(N-max(1, len(T)-2*K))

Solution (Python):
def generate_block(s):
    table = []
    j = 0
    for i in range(len(s)-1):
        if s[i] != s[i+1]:
            table.append(s[j:i+1])
            j = i + 1
    return table

def happy(s):
    count = 0
    ll = len(s)
    for i in range(ll):
        if i != ll-1 and s[i] == 'R' and s[i+1] == 'R': count += 1
        if i != 0 and s[i] == 'L' and s[i-1] == 'L': count += 1
    return count

n, k = map(int, input().split())
s = input() + '.'
x = 0
table = generate_block(s)[::-1]
while k > 0:
    pos = 2 * x + 1
    if pos >= len(table): break
    if 'L' in table[pos]: table[pos] = table[pos].replace('L', 'R')
    else: table[pos] = table[pos].replace('R', 'L')
    k -= 1
    x += 1
print(happy(''.join(table)))

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())

def main():
    N, K = mis()
    S = input()
    ans = min(sum(S[i]==S[i+1] for i in range(N-1)) + 2*K, N-1)
    print(ans)

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

Solution (Python):
N, K = map(int,input().split())
S = input()
count_RL = 0
for i in range(N-1):
    if S[i] == 'R' and S[i+1] == 'L':
        count_RL += 1
count_edge = 0
if S[0] == 'L':
    count_edge += 1
if S[N-1] == 'R':
    count_edge += 1
count_K = 0
for i in range(K):
    if count_RL == 0:
        break
    count_RL -= 1
    count_K += 1
if (K - count_K) > 0:
    count = K - count_K
    if count_edge == 2:
        count_edge -= 1
if count_edge == 0 and count_RL == 0:
    count_edge = 1
    
not_happy = N - 2*count_RL - count_edge
print(not_happy)

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


def counthappy(s):
    happy = 0
    for i in range(0, len(s)):
        if i != 0 and s[i] == 'L':
            if s[i - 1] == 'L':
                happy += 1
        elif i != len(s) - 1 and s[i] == 'R':
            if s[i + 1] == 'R':
                happy += 1
    return happy


flag = 0
S = S + 'J'
s_ = []
for i in range(0, N):
    if S[i] != S[i + 1]:
        s_.append(S[flag:i + 1])
        flag = i + 1

count = 0
for i in range(1, len(s_), 2):
    count += 1
    if s_[i][0] == 'L':
        s_[i] = s_[i].replace('L', 'R')
    elif s_[i][0] == 'R':
        s_[i] = s_[i].replace('R', 'L')

    if count == K:
        break

s_join = ''.join(s_)

happy = counthappy(s_join)
print(happy)