Problem p02918 (python)

Fixed submission:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n = I()
    print(n**3)
    return

#B
def B():
    n = I()
    a = LI()
    b = LI()
    c = LI()
    ans = sum(b)
    for i in range(n-1):
        x = a[i]
        y = a[i+1]
        if y == x+1:
            ans += c[x-1]
    print(ans)
    return

#C
def C():
    n = I()
    b = LI()
    b.append(float("inf"))
    a = [min(b[i],b[i-1]) for i in range(n)]
    print(sum(a))
    return

#D
def D():
    n,k = LI()
    a = S()
    ans = 0
    for i in range(n-1):
        if a[i] == a[i+1]:
            ans += 1
    print(min(n-1,ans+2*k))        
    return

#E
def E():
    def add(i):
        while i <= n:
            bit[i] += 1
            i += i&-i

    def sum(i):
        res = 0
        while i > 0:
            res += bit[i]
            i -= i&-i
        return res

    n = I()
    p = LI()
    r = [0]*n
    bit = [0]*(n+1)
    for i in range(n)[::-1]:
        r[i] = (n-i-1)-sum(p[i])
        add(p[i])
    print(r)
    l = [0]*n
    bit = [0]*(n+1)
    for i in range(n):
        l[i] = i-sum(p[i])
        add(p[i])
    print(l)

    return

#F
def F():
    n = I()
    s = LI()
    s.sort()
    s = s[::-1]
    if s[0] == s[1]:
        print("No")
        return
    a = [s[0]]
    j = 1
    s.append(0)
    q = deque()
    for _ in range(n):
        for i in range(1<<_):
            if a[i] > s[j]:
                a.append(s[j])
                j += 1
            else:
                q.append(a[i])
    print(a)
    return

#Solve
if __name__ == "__main__":
    D()

Buggy submission (verdict: Runtime Error):
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n = I()
    print(n**3)
    return

#B
def B():
    n = I()
    a = LI()
    b = LI()
    c = LI()
    ans = sum(b)
    for i in range(n-1):
        x = a[i]
        y = a[i+1]
        if y == x+1:
            ans += c[x-1]
    print(ans)
    return

#C
def C():
    n = I()
    b = LI()
    b.append(float("inf"))
    a = [min(b[i],b[i-1]) for i in range(n)]
    print(sum(a))
    return

#D
def D():
    n,k = LI()
    a = S()
    if a.count("L") in [0,n]:
        print(n-1)
        return
    if n == 1:
        print(0)
        return
    if n == 2:
        print(1)
        return
    ans = 0
    f = [0]*n
    for i in range(n-1):
        if a[i] == a[i+1]:
            ans += 1
            if a[i] == "L":
                f[i+1] = 1
            else:
                f[i] = 1
    f.append(0)
    f.append(0)
    d = defaultdict(lambda : 0)
    l = 0
    s = a[0]
    for i in range(n):
        if a[i] != s:
            d[(l,i-1)] = 1
            s = a[i]
            l = i
    r = n-1
    l = 0
    m = 0
    for i in range(k):
        if not i%2:
            while a[r] != "R":
                r -= 1
            while a[l] != "R":
                l += 1
        else:
            while a[r] != "L":
                r -= 1
            while a[l] != "L":
                l += 1
        if d[(l,r)]:
            print(n-1)
            return
        else:
            m += 2-f[l-1]-f[r+1]
    r = n-1
    l = 0
    m_ = 0
    for i in range(k):
        if i%2:
            while a[r] != "R":
                r -= 1
            while a[l] != "R":
                l += 1
        else:
            while a[r] != "L":
                r -= 1
            while a[l] != "L":
                l += 1
        if d[(l,r)]:
            print(n-1)
            return
        else:
            m_ += 2-f[l-1]-f[r+1]
    print(ans+max(m,m_))
    return

#E
def E():
    def add(i):
        while i <= n:
            bit[i] += 1
            i += i&-i

    def sum(i):
        res = 0
        while i > 0:
            res += bit[i]
            i -= i&-i
        return res

    n = I()
    p = LI()
    r = [0]*n
    bit = [0]*(n+1)
    for i in range(n)[::-1]:
        r[i] = (n-i-1)-sum(p[i])
        add(p[i])
    print(r)
    l = [0]*n
    bit = [0]*(n+1)
    for i in range(n):
        l[i] = i-sum(p[i])
        add(p[i])
    print(l)

    return

#F
def F():

    return

#Solve
if __name__ == "__main__":
    D()

Buggy submission (verdict: Runtime Error):
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n = I()
    print(n**3)
    return

#B
def B():
    n = I()
    a = LI()
    b = LI()
    c = LI()
    ans = sum(b)
    for i in range(n-1):
        x = a[i]
        y = a[i+1]
        if y == x+1:
            ans += c[x-1]
    print(ans)
    return

#C
def C():
    n = I()
    b = LI()
    b.append(float("inf"))
    a = [min(b[i],b[i-1]) for i in range(n)]
    print(sum(a))
    return

#D
def D():
    n,k = LI()
    a = S()
    if a.count("L") in [0,n]:
        print(n-1)
        return
    if n == 1:
        print(0)
        return
    if n == 2:
        print(1)
        return
    ans = 0
    f = [0]*n
    for i in range(n-1):
        if a[i] == a[i+1]:
            ans += 1
            if a[i] == "L":
                f[i+1] = 1
            else:
                f[i] = 1
    f.append(0)
    f.append(0)
    r = n-1
    l = 0
    m = 0
    for i in range(k):
        if not i%2:
            while a[r] != "R":
                r -= 1
            while a[l] != "R":
                l += 1
        else:
            while a[r] != "L":
                r -= 1
            while a[l] != "L":
                l += 1
        if r <= l:
            print(n-1)
            return
        else:
            m += 2-f[l-1]-f[r+1]
    r = n-1
    l = 0
    m_ = 0
    for i in range(k):
        if i%2:
            while a[r] != "R":
                r -= 1
            while a[l] != "R":
                l += 1
        else:
            while a[r] != "L":
                r -= 1
            while a[l] != "L":
                l += 1
        if r <= l:
            print(n-1)
            return
        else:
            m_ += 2-f[l-1]-f[r+1]
    print(ans+max(m,m_))
    return

#E
def E():
    def add(i):
        while i <= n:
            bit[i] += 1
            i += i&-i

    def sum(i):
        res = 0
        while i > 0:
            res += bit[i]
            i -= i&-i
        return res

    n = I()
    p = LI()
    r = [0]*n
    bit = [0]*(n+1)
    for i in range(n)[::-1]:
        r[i] = (n-i-1)-sum(p[i])
        add(p[i])
    print(r)
    l = [0]*n
    bit = [0]*(n+1)
    for i in range(n):
        l[i] = i-sum(p[i])
        add(p[i])
    print(l)

    return

#F
def F():

    return

#Solve
if __name__ == "__main__":
    D()