Problem p03353 (python)

Fixed submission:
s = input()
K = int(input())

subset_list = list(s)
subset_list += [s[i:i+2] for i in range(len(s)-1)]
subset_list += [s[i:i+3] for i in range(len(s)-2)]
subset_list += [s[i:i+4] for i in range(len(s)-3)]
subset_list += [s[i:i+5] for i in range(len(s)-4)]
subset_list = list(set(subset_list))
subset_list.sort()

print(subset_list[K-1])

Buggy submission (verdict: Runtime Error):
s = input()
K = int(input())

str_list = list(s)
str_list2 = list(s)
str_list2 = list(set(str_list2))
str_list2.sort()
a = str_list2[0]
n = str_list.count(a)

if K==1:
    print(a)
else:
    K -= 1
    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-1]
    if len(indexes) >0:
        sub_list = [s[i:i+2] for i in indexes]
    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-2]
    if len(indexes) >0:
        sub_list += [s[i:i+3] for i in indexes]
    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-3]
    if len(indexes) >0:
        sub_list += [s[i:i+4] for i in indexes]
    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-4]
    if len(indexes) >0:
        sub_list += [s[i:i+5] for i in indexes]
    sub_list = list(set(sub_list))
    sub_list.sort()
    if len(sub_list) >= K:
        print(sub_list[K-1])
    else:
        K -= len(sub_list)
        a = str_list[1]
        if K==1:
            print(a)
        else:
            K -= 1
            sub_list = []
            indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-1]
            if len(indexes) >0:
                sub_list = [s[i:i+2] for i in indexes]
            indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-2]
            if len(indexes) >0:
                sub_list += [s[i:i+3] for i in indexes]
            indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-3]
            if len(indexes) >0:
                sub_list += [s[i:i+4] for i in indexes]
            sub_list = list(set(sub_list))
            sub_list.sort()
            if len(sub_list) >= K:
                print(sub_list[K-1])
            else:
                K -= len(sub_list)
                a = str_list[2]
                if K==1:
                    print(a)
                else:
                    K -=1
                    sub_list = []
                    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-1]
                    if len(indexes) >0:
                        sub_list = [s[i:i+2] for i in indexes]
                    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-2]
                    if len(indexes) >0:
                        sub_list += [s[i:i+3] for i in indexes]
                    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-3]
                    if len(indexes) >0:
                        sub_list += [s[i:i+4] for i in indexes]
                    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-4]
                    sub_list = list(set(sub_list))
                    sub_list.sort()
                    print(sub_list[K-1])

Buggy submission (verdict: Runtime Error):
s = input()
K = int(input())
 
str_list = list(s)
str_list2 = list(s)
str_list2 = list(set(str_list2))
str_list2.sort()
a = str_list2[0]
n = str_list.count(a)
 
if K==1:
    print(a)
else:
    K -= 1
    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-1]
    if len(indexes) >0:
        sub_list = [s[i:i+2] for i in indexes]
    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-2]
    if len(indexes) >0:
        sub_list += [s[i:i+3] for i in indexes]
    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-3]
    if len(indexes) >0:
        sub_list += [s[i:i+4] for i in indexes]
    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-4]
    if len(indexes) >0:
        sub_list += [s[i:i+5] for i in indexes]
    sub_list = list(set(sub_list))
    sub_list.sort()
    if len(sub_list) >= K:
        print(sub_list[K-1])
    else:
        K -= len(sub_list)
        a = str_list[1]
        if K==1:
            print(a)
        else:
            K -= 1
            indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-1]
            if len(indexes) >0:
                sub_list = [s[i:i+2] for i in indexes]
            indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-2]
            if len(indexes) >0:
                sub_list += [s[i:i+3] for i in indexes]
            indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-3]
            if len(indexes) >0:
                sub_list += [s[i:i+4] for i in indexes]
            indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-4]
            sub_list = list(set(sub_list))
            sub_list.sort()
            if len(sub_list) >= K:
                print(sub_list[K-1])
            else:
                K -= len(sub_list)
                a = str_list[2]
                if K==1:
                    print(a)
                else:
                    K -=1
                    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-1]
                    if len(indexes) >0:
                        sub_list = [s[i:i+2] for i in indexes]
                    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-2]
                    if len(indexes) >0:
                        sub_list += [s[i:i+3] for i in indexes]
                    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-3]
                    if len(indexes) >0:
                        sub_list += [s[i:i+4] for i in indexes]
                    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-4]
                    sub_list = list(set(sub_list))
                    sub_list.sort()
                    print(sub_list[K-1])

Buggy submission (verdict: Runtime Error):
s = input()
K = int(input())

str_list = list(s)
str_list2 = list(s)
str_list2 = list(set(str_list2))
str_list2.sort()
print(str_list)
a = str_list2[0]
n = str_list.count(a)

if K==1:
    print(a)
else:
    K -= 1
    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-1]
    if len(indexes) >0:
        sub_list = [s[i:i+2] for i in indexes]
    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-2]
    if len(indexes) >0:
        sub_list += [s[i:i+3] for i in indexes]
    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-3]
    if len(indexes) >0:
        sub_list += [s[i:i+4] for i in indexes]
    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-4]
    if len(indexes) >0:
        sub_list += [s[i:i+5] for i in indexes]
    sub_list = list(set(sub_list))
    sub_list.sort()
    if len(sub_list) >= K:
        print(sub_list[K-1])
    else:
        K -= len(sub_list)
        a = str_list[1]
        if K==1:
            print(a)
        else:
            K -= 1
            indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-1]
            if len(indexes) >0:
                sub_list = [s[i:i+2] for i in indexes]
            indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-2]
            if len(indexes) >0:
                sub_list += [s[i:i+3] for i in indexes]
            indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-3]
            if len(indexes) >0:
                sub_list += [s[i:i+4] for i in indexes]
            indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-4]
            sub_list = list(set(sub_list))
            sub_list.sort()
            if len(sub_list) >= K:
                print(sub_list[K-1])
            else:
                K -= len(sub_list)
                a = str_list[2]
                if K==1:
                    print(a)
                else:
                    K -=1
                    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-1]
                    if len(indexes) >0:
                        sub_list = [s[i:i+2] for i in indexes]
                    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-2]
                    if len(indexes) >0:
                        sub_list += [s[i:i+3] for i in indexes]
                    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-3]
                    if len(indexes) >0:
                        sub_list += [s[i:i+4] for i in indexes]
                    indexes = [i for i, x in enumerate(str_list) if x == a and i <len(s)-4]
                    sub_list = list(set(sub_list))
                    sub_list.sort()
                    print(sub_list[K-1])