Problem p03393 (python)

Fixed submission:
def next_permutation(case):
    for index in range(1, len(case)):
        Px_index = len(case) - 1 - index
        # Start travelling from the end of the Data Structure
        Px = case[-index - 1]
        Px_1 = case[-index]

        # Search for a pair where latter the is greater than prior
        if Px < Px_1:
            suffix = case[-index:]
            pivot = Px
            minimum_greater_than_pivot_suffix_index = -1
            suffix_index = 0

            # Find the index inside the suffix where ::: [minimum value is greater than the pivot]
            for Py in suffix:
                if pivot < Py:
                    if minimum_greater_than_pivot_suffix_index == -1 or \
                            suffix[minimum_greater_than_pivot_suffix_index] >= Py:
                        minimum_greater_than_pivot_suffix_index = suffix_index
                suffix_index += 1
            # index in the main array
            minimum_greater_than_pivot_index = minimum_greater_than_pivot_suffix_index + Px_index + 1

            # SWAP
            temp = case[minimum_greater_than_pivot_index]
            case[minimum_greater_than_pivot_index] = case[Px_index]
            case[Px_index] = temp

            # Sort suffix
            new_suffix = case[Px_index + 1:]
            new_suffix.sort()

            # Build final Version
            new_prefix = case[:Px_index + 1]
            next_permutation = new_prefix + new_suffix
            return next_permutation
        elif index == (len(case) - 1):
            # This means that this is at the highest possible lexicographic order
            return False


s = input()
if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) != 26:
    used = [False] * 26
    for i in range(len(s)):
        used[ord(s[i]) - ord("a")] = True
    for i in range(26):
        if used[i] is False:
            s += chr(ord("a") + i)
            print(s)
            break
else:
    dat = []
    for i in range(len(s)):
        dat.append(ord(s[i]) - ord("a"))
    #print(dat)
    #print(next_permutation(dat))
    dat = next_permutation(dat)
    res = ""
    t = "".join(list(map(lambda x: chr(ord("a") + x), dat)))
    for i in range(26):
        print(t[i], end="")
        if s[i] != t[i]:
            break
    print("")

Buggy submission (verdict: Runtime Error):
s = input()
if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) != 26:
    used = [False] * 26
    for i in range(len(s)):
        used[ord(s[i]) - ord("a")] = True
    for i in range(26):
        if used[i] is False:
            s += chr(ord("a") + i)
            print(s)
            break
else:
    ss = []
    s = list(s)
    done = False
    used = [False] * 26
    for i in range(25):
        #print("load", s[i])
        if s[i] > s[i+1]:
            used[ord(s[i]) - ord("a")] = True
            #print(s[i])
            for i in range(26):
                if used[i] is False:
                    ss[-1] = chr(ord("a") + i)
                    print("".join(ss))
                    done = True
                    break
                if done:
                    break
        else:
            ss += s[i]
            used[ord(s[i]) - ord("a")] = True

Buggy submission (verdict: Runtime Error):
import sys
calcLower2Index = lambda x: ord(x) - ord("a")
calcUpper2Index = lambda x: ord(x) - ord("A")
calcIndex2Lower = lambda x: chr(ord("a") + x)
calcIndex2Upper = lambda x: chr(ord("A") + x)
s = input()
f = [False] * 26
l = len(s)
if  l == 26: #full
    res = []
    if s == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        m = -1 # これまでのmax
        ind = -1
        endf = False
        for i in range(l):
            ch = calcLower2Index(s[i])
            f[ch] = True
            ind += 1
            if m > ch:
                for k in range(26):
                    if f[k] is False:
                        t = k
                        break
                res[-2] = calcIndex2Lower(t)
                print("".join(res[:-1]))
                endf = True
                break
            else:
                m = ch
                res.append(calcIndex2Lower(ch))
            if endf:
                break

else: # under 26
    for i in range(l):
        c = calcLower2Index(s[i])
        f[c] = True
    can = ""
    for i in range(26):
        if f[i] is False:
            can = calcIndex2Lower(i)
            break
    print(s + can)

Buggy submission (verdict: Runtime Error):
import sys
calcLower2Index = lambda x: ord(x) - ord("a")
calcUpper2Index = lambda x: ord(x) - ord("A")
calcIndex2Lower = lambda x: chr(ord("a") + x)
calcIndex2Upper = lambda x: chr(ord("A") + x)
s = input()
f = [False] * 26
l = len(s)
if  l == 26: #full
    res = []
    if s == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
        sys.exit(0)
    m = -1 # これまでのmax
    ind = -1
    for i in range(l):
        ch = calcLower2Index(s[i])
        f[ch] = True
        ind += 1
        if m > ch:
            for k in range(26):
                if f[k] is False:
                    t = k
                    break
            res[-2] = calcIndex2Lower(t)
            print("".join(res[:-1]))
            sys.exit(0)
        else:
            m = ch
            res.append(calcIndex2Lower(ch))

else: # under 26
    for i in range(l):
        c = calcLower2Index(s[i])
        f[c] = True
    can = ""
    for i in range(26):
        if f[i] is False:
            can = calcIndex2Lower(i)
            break
    print(s + can)

Buggy submission (verdict: Runtime Error):
calcLower2Index = lambda x: ord(x) - ord("a")
calcUpper2Index = lambda x: ord(x) - ord("A")
calcIndex2Lower = lambda x: chr(ord("a") + x)
calcIndex2Upper = lambda x: chr(ord("A") + x)
s = input()
f = [False] * 26
l = len(s)
if  l == 26: #full
    res = []
    if s == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
        return
    m = -1 # これまでのmax
    ind = -1
    for i in range(l):
        ch = calcLower2Index(s[i])
        f[ch] = True
        ind += 1
        if m > ch:
            for k in range(26):
                if f[k] is False:
                    t = k
                    break
            res[-2] = calcIndex2Lower(t)
            print("".join(res[:-1]))
            return
        else:
            m = ch
            res.append(calcIndex2Lower(ch))

else: # under 26
    for i in range(l):
        c = calcLower2Index(s[i])
        f[c] = True
    can = ""
    for i in range(26):
        if f[i] is False:
            can = calcIndex2Lower(i)
            break
    print(s + can)