Problem p03393 (python)

Fixed submission:
S = input()

# 1 <= |S| <= 26
# lowercase

import sys
import copy

def next_permutation(a):
    for i in reversed(range(len(a) - 1)):
        if a[i] < a[i + 1]:
            break
    else:
        return False

    j = next(j for j in reversed(range(i + 1, len(a))) if a[i] < a[j])
    a[i], a[j] = a[j], a[i]
    a[i + 1:] = reversed(a[i + 1:])
    return True

if len(S) == 26:
    sl = list(S)
    if next_permutation(sl):
        np = "".join(sl)
        for b in range(0, 26):
            if np[b] != S[b]:
                print(np[:b+1])
                exit()

    else:
        print(-1)
        exit()
else:
    hist = set(S)
    for c in range(0, 27):
        cand = chr(c+ord('a'))
        if cand not in hist:
            print(S+cand)
            sys.exit()

Buggy submission (verdict: Runtime Error):
S = input()

# 1 <= |S| <= 26
# lowercase

import sys
import copy

if len(S) == 26:
    if S == "zyxwvutsrqponmlkjihgfedcba":
        ans = "-1"
        print(ans)
        sys.exit()

    else:
        original = copy.copy(S)
        np = ""
        for i in range(0, len(S)-2):
            # print(i)
            if S[i] < S[i+1]:
                np += S[i]

            else:
                i = i - 1
                for j in reversed(range(i, len(S))):
                    # print("checking:", j)
                    # print("S[i], S[j]", S[i], ",", S[j])
                    if S[i] < S[j]:
                        np = np[:-1] + S[j]
                        tmp = S[i]
                        S = S[:i-1] + S[j] + S[i+1:]
                        S = S[:j-1] + tmp + S[j+1:]
                        # S[i+1:] = S[:i] + "".join(list(reversed(S[i+1:])))
                        # S = S[:i] + 
                        np += "".join(list(reversed(S[i+1:])))
                        break

        # print("np", np)
        for b in range(0, 27):
            if np[b] != original[b]:
                print(np[:b+1])
                exit()

        print(S)

            
else:
    hist = set(S)
    for c in range(0, 27):
        cand = chr(c+ord('a'))
        if cand not in hist:
            print(S+cand)
            sys.exit()

Buggy submission (verdict: Runtime Error):
S = input()

# 1 <= |S| <= 26
# lowercase

import sys
import copy

if len(S) == 26:
    if S == "zyxwvutsrqponmlkjihgfedcba":
        ans = "-1"
        print(ans)
        sys.exit()

    else:
        original = copy.copy(S)
        np = ""
        for i in range(0, len(S)-2):
            print(i)
            if S[i] < S[i+1]:
                np += S[i]

            else:
                i = i - 1
                for j in reversed(range(i, len(S))):
                    # print("checking:", j)
                    # print("S[i], S[j]", S[i], ",", S[j])
                    if S[i] < S[j]:
                        np = np[:-1] + S[j]
                        tmp = S[i]
                        S = S[:i-1] + S[j] + S[i+1:]
                        S = S[:j-1] + tmp + S[j+1:]
                        # S[i+1:] = S[:i] + "".join(list(reversed(S[i+1:])))
                        # S = S[:i] + 
                        np += "".join(list(reversed(S[i+1:])))
                        break

        # print("np", np)
        for b in range(0, 27):
            if np[b] != original[b]:
                print(np[:b+1])
                exit()

        print(S)

            
else:
    hist = set(S)
    for c in range(0, 27):
        cand = chr(c+ord('a'))
        if cand not in hist:
            print(S+cand)
            sys.exit()

Buggy submission (verdict: Runtime Error):
S = input()

# 1 <= |S| <= 26
# lowercase

import sys
import copy

if len(S) == 26:
    if S == "zyxwvutsrqponmlkjihgfedcba":
        ans = "-1"
        print(ans)
        sys.exit()

    else:
        original = copy.copy(S)
        np = ""
        for i in range(0, len(S)-2):
            print(i)
            if S[i] < S[i+1]:
                np += S[i]

            else:
                i = i - 1
                for j in reversed(range(i, len(S))):
                    # print("checking:", j)
                    # print("S[i], S[j]", S[i], ",", S[j])
                    if S[i] < S[j]:
                        np = np[:-1] + S[j]
                        tmp = S[i]
                        S = S[:i-1] + S[j] + S[i+1:]
                        S = S[:j-1] + tmp + S[j+1:]
                        # S[i+1:] = S[:i] + "".join(list(reversed(S[i+1:])))
                        # S = S[:i] + 
                        np += "".join(list(reversed(S[i+1:])))
                        break

        print("np", np)
        for b in range(0, 27):
            if np[b] != original[b]:
                print(np[:b+1])
                exit()

        print(S)

            
else:
    hist = set(S)
    for c in range(0, 27):
        cand = chr(c+ord('a'))
        if cand not in hist:
            print(S+cand)
            sys.exit()

Buggy submission (verdict: Runtime Error):
S = input()

import sys
import copy

if len(S) == 26:
    if S == "zyxwvutsrqponmlkjihgfedcba":
        ans = "-1"
        print(ans)
        sys.exit()

    else:
        original = copy.copy(S)
        np = ""
        for i in range(0, len(S)-2):
            print(i)
            if S[i] < S[i+1]:
                np += S[i]

            else:
                i = i - 1
                for j in reversed(range(i, len(S))):
                    if S[i] < S[j]:
                        np = np[:-1] + S[j]
                        tmp = S[i]
                        S = S[:i-1] + S[j] + S[i+1:]
                        S = S[:j-1] + tmp + S[j+1:]
                        np += "".join(list(reversed(S[i+1:])))
                        break

        print("np", np)
        for b in range(0, 27):
            if np[b] != original[b]:
                print(np[:b+1])
                exit()

        print(S)

            
else:
    hist = set(S)
    for c in range(0, 27):
        cand = chr(c+ord('a'))
        if cand not in hist:
            print(S+cand)
            sys.exit()