Problem p02918 (python)

Fixed submission:
n, k = map(int, input().split())
s = input()
s = list(s)

seqCnt = 0
seqTotal = 0
for i in range(n-1) :
    #print(i, seqCnt)
    if s[i] == s[i+1] :
        seqCnt += 1
    if s[i] != s[i+1] or i == n - 2 :
        #print(ans, seqCnt, i)
        seqTotal += seqCnt
        seqCnt = 0

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

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
s = input()
s = list(s)
#print(s)
before = s[1]
flag = False
place = -1
end = -1
cnt = 0

for i in range(1, n-1) :

    if s[i] != before :
        flag = True
        place = i

    if flag :
        #print(s, i)
        if i + 1 == n - 1 :
            if s[place] == s[i+1] :
                end = i+2
            else :
                end = i+1
            #print(s, i, end, 'end')
            if s[place] == 'L' :
                if end == i+1 :
                    s[place:end] = 'R'
                else :
                    for j in range(place, n) :
                        s[j] = 'R'
            else :
                if end == i+1 :
                    s[place:end] = 'L'
                else :
                    for j in range(place, n) :
                        s[j] = 'L'
            #print(s, 'end2')
            cnt += 1
            if cnt == k :
                break
            flag = False
            before = s[i]
            continue

        if s[i] != s[i+1] :
            #print(s)
            if s[place] == 'L' :
                s[place:i+1] = 'R'
            else :
                s[place:i+1] = 'L'

            cnt += 1
            #print(s)
            if cnt == k:
                break
            flag = False
            before = s[i]
            continue
    before = s[i]

#print(s)
if cnt != k :
    if s[0] != s[1] :
        s[0] = s[1]
    if k >= cnt + 2 :
        if s[n-1] != s[n-2] :
            s[n-1] = s[n-2]

seqCnt = 0
ans = 0
#print(s)
for i in range(n-1) :
    #print(i, seqCnt)
    if s[i] == s[i+1] :
        seqCnt += 1
    if s[i] != s[i+1] or i == n - 2 :
        #print(ans, seqCnt, i)
        if seqCnt >= 1 :
            ans += seqCnt
        seqCnt = 0

print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
s = input()
s = list(s)
print(s)
before = s[1]
flag = False
place = -1
end = -1
cnt = 0

for i in range(1, n-1) :

    if s[i] != before :
        flag = True
        place = i

    if flag :
        print(s, i)
        if i + 1 == n - 1 :
            if s[place] == s[i+1] :
                end = i+1
            else :
                end = i
            print(s, i, end, 'end')
            if s[place] == 'L' :
                if end == i :
                    s[place:end] = 'R'
                else :
                    s[place:end] = 'R'
                    #s[end-1] ='R'
            else :
                if end == i :
                    s[place:end] = 'L'
                else :
                    s[place:end] = 'L'
                    #s[end-1] = 'L'
            print(s, 'end2')
            cnt += 1
            if cnt == k :
                break
            flag = False
            before = s[i]
            continue

        if s[i] != s[i+1] :
            print(s)
            if s[place] == 'L' :
                s[place:i+1] = 'R'
            else :
                s[place:i+1] = 'L'

            cnt += 1
            print(s)
            if cnt == k:
                break
            flag = False
            before = s[i]
            continue
    before = s[i]

print(s)
if cnt != k :
    if s[0] != s[1] :
        s[0] = s[1]
    if k >= cnt + 2 :
        if s[n-1] != s[n-2] :
            s[n-1] = s[n-2]

seqCnt = 0
ans = 0
print(s)
for i in range(n-1) :
    print(i, seqCnt)
    if s[i] == s[i+1] :
        seqCnt += 1
    if s[i] != s[i+1] or i == n - 2 :
        print(ans, seqCnt, i)
        if seqCnt >= 1 :
            ans += seqCnt
        seqCnt = 0

print(ans)