Buggy python submission (verdict: Time Limit Exceeded):
def kaiten(parts):
    parts = reversed(parts)
    parts = ''.join(parts)
    parts = parts.replace('L', 'Z')
    parts = parts.replace('R', 'L')
    parts = parts.replace('Z', 'R')
    return list(parts)

def occurrences(string, sub):
    count = start = 0
    while True:
        start = string.find(sub, start) + 1
        if start > 0:
            count += 1
        else:
            return count

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

for _ in range(k):
    l = s.find('LR')
    r = s.find('RL', l)
    l2 = s.find('RL')
    r2 = s.find('LR', l2)
    if l == r == l2 == r2 == -1:
        break
    _s = list(s)
    if 0 <= l and l < r:
        p = _s[l+1:r+1]
        _s[l+1:r+1] = kaiten(p)
    elif 0 <= l2 and l2 < r2:
        p = _s[l2+1:r2+1]
        _s[l2+1:r2+1] = kaiten(p)
    elif 0 <= l:
        p = _s[l+1:]
        _s[l+1:] = kaiten(p)
    elif 0 <= r:
        p = _s[:r+1]
        _s[:r+1] = kaiten(p)
    elif 0 <= l2:
        p = _s[l2+1:]
        _s[l2+1:] = kaiten(p)
    elif 0 <= r2:
        p = _s[:r2+1]
        _s[:r2+1] = kaiten(p)
    s = ''.join(_s)

score = occurrences(s, 'LL') + occurrences(s, 'RR')
print(score)


Fixed submission:
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(h + 2 * k, n - 1))

Buggy python submission (verdict: Time Limit Exceeded):
def kaiten(parts):
    parts = reversed(parts)
    parts = ''.join(parts)
    parts = parts.replace('L', 'Z')
    parts = parts.replace('R', 'L')
    parts = parts.replace('Z', 'R')
    return list(parts)

def occurrences(string, sub):
    count = start = 0
    while True:
        start = string.find(sub, start) + 1
        if start > 0:
            count += 1
        else:
            return count

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

for _ in range(k):
    l = s.find('LR')
    r = s.find('RL', l)
    l2 = s.find('RL')
    r2 = s.find('LR', l2)
    _s = list(s)
    if 0 <= l and l < r:
        p = _s[l+1:r+1]
        _s[l+1:r+1] = kaiten(p)
    elif 0 <= l2 and l2 < r2:
        p = _s[l2+1:r2+1]
        _s[l2+1:r2+1] = kaiten(p)
    elif 0 <= l:
        p = _s[l+1:]
        _s[l+1:] = kaiten(p)
    elif 0 <= r:
        p = _s[:r+1]
        _s[:r+1] = kaiten(p)
    elif 0 <= l2:
        p = _s[l2+1:]
        _s[l2+1:] = kaiten(p)
    elif 0 <= r2:
        p = _s[:r2+1]
        _s[:r2+1] = kaiten(p)
    s = ''.join(_s)

score = occurrences(s, 'LL') + occurrences(s, 'RR')
print(score)


Fixed submission:
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(h + 2 * k, n - 1))

Buggy python submission (verdict: Time Limit Exceeded):
def kaiten(parts):
    _parts = []
    for i in range(len(parts)):
        _parts.append('L' if parts[-1-i] == 'R' else 'R')
    return _parts

def occurrences(string, sub):
    count = start = 0
    while True:
        start = string.find(sub, start) + 1
        if start > 0:
            count += 1
        else:
            return count

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

for _ in range(k):
    l = s.find('LR')
    r = s.find('RL', l)
    l2 = s.find('RL')
    r2 = s.find('LR', l2)
    _s = list(s)
    if 0 <= l and l < r:
        p = _s[l+1:r+1]
        _s[l+1:r+1] = kaiten(p)
    elif 0 <= l2 and l2 < r2:
        p = _s[l2+1:r2+1]
        _s[l2+1:r2+1] = kaiten(p)
    elif 0 <= l:
        p = _s[l+1:]
        _s[l+1:] = kaiten(p)
    elif 0 <= r:
        p = _s[:r+1]
        _s[:r+1] = kaiten(p)
    elif 0 <= l2:
        p = _s[l2+1:]
        _s[l2+1:] = kaiten(p)
    elif 0 <= r2:
        p = _s[:r2+1]
        _s[:r2+1] = kaiten(p)
    s = ''.join(_s)

score = occurrences(s, 'LL') + occurrences(s, 'RR')
print(score)


Fixed submission:
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(h + 2 * k, n - 1))

Buggy python submission (verdict: Time Limit Exceeded):
def kaiten(parts):
    _parts = []
    for i in range(len(parts)):
        _parts.append('L' if parts[-1-i] == 'R' else 'R')
    return _parts

def occurrences(string, sub):
    count = start = 0
    while True:
        start = string.find(sub, start) + 1
        if start > 0:
            count += 1
        else:
            return count

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

for _ in range(k):
    l = s.find('LR')
    r = s.find('RL', l)
    l2 = s.find('RL')
    r2 = s.find('LR', l2)
    _s = list(s)
    if 0 <= l and l < r:
        p = _s[l+1:r+1]
        _s[l+1:r+1] = kaiten(p)
    elif 0 <= l2 and l2 < r2:
        p = _s[l2+1:r2+1]
        _s[l2+1:r2+1] = kaiten(p)
    elif 0 <= l:
        p = _s[l+1:]
        _s[l+1:] = kaiten(p)
    elif 0 <= r:
        p = _s[:r+1]
        _s[:r+1] = kaiten(p)
    elif 0 <= l2:
        p = _s[l2+1:]
        _s[l2+1:] = kaiten(p)
    elif 0 <= r2:
        p = _s[:r2+1]
        _s[:r2+1] = kaiten(p)
    s = ''.join(_s)

score = occurrences(s, 'LL') + occurrences(s, 'RR')
print(score)


Fixed submission:
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(h + 2 * k, n - 1))