Prinzessin der Verurteilung (Codeforces 1536B)
Tags: Others, strings

I, Fischl, Prinzessin der Verurteilung, descend upon this land by the call of fate an — Oh, you are also a traveler from another world? Very well, I grant you permission to travel with me.It is no surprise Fischl speaks with a strange choice of words. However, this time, not even Oz, her raven friend, can interpret her expressions! Maybe you can help us understand what this young princess is saying?You are given a string of $$$n$$$ lowercase Latin letters, the word that Fischl just spoke. You think that the MEX of this string may help you find the meaning behind this message. The MEX of the string is defined as the shortest string that doesn't appear as a contiguous substring in the input. If multiple strings exist, the lexicographically smallest one is considered the MEX. Note that the empty substring does NOT count as a valid MEX.A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds:   $$$a$$$ is a prefix of $$$b$$$, but $$$a \ne b$$$;  in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$. A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.Find out what the MEX of the string is!

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 1000$$$). Description of the test cases follows.The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the length of the word. The second line for each test case contains a single string of $$$n$$$ lowercase Latin letters.The sum of $$$n$$$ over all test cases will not exceed $$$1000$$$.

Output: For each test case, output the MEX of the string on a new line.


Sample Inputs:
3
28
qaabzwsxedcrfvtgbyhnujmiklop
13
cleanairactbd
10
aannttoonn

Sample Outputs:
ac
f
b

Solution (Python):
T = int(input())
for _ in range(T):
    n = int(input())
    str = input()
    tmp = 'a'
    flag = False
    for i in range(0, 26):
        ch = chr(ord(tmp) + i)
        if ch not in str:
            print(ch)
            flag = True
            break
    if flag:
        flag = False
        continue
    tmp = ''
    ch1 = 'a'
    ch2 = 'a'
    for i in range(0, 26):
        for j in range(0, 26):
            tmp = chr(ord(ch1) + i) + chr(ord(ch2) + j)
            if tmp not in str:
                print(tmp)
                flag = True
                break
        if flag:
            break
    if flag:
        flag = False
        continue
    tmp = ''
    ch1 = 'a'
    ch2 = 'a'
    ch3 = 'a'
    for i in range(0, 26):
        for j in range(0, 26):
            for k in range(0, 26):
                tmp = chr(ord(ch1) + i) + chr(ord(ch2) + j) + chr(ord(ch3) + k)
                if tmp not in str:
                    print(tmp)
                    flag = True
                    break
            if flag:
                break
        if flag:
            break
    if flag:
        continue

Solution (Python):
t = int(input())
for _ in range(t):
    l = [False] * 26
    n = int(input())
    word = input()
    for i in word:
        l[ord(i) - ord('a')] = True
    flag = False
    ans = ''
    for i in range(26):
        if not l[i]:
            ans = chr(i + 97)
            flag = True
            break
    if flag:
        print(ans)
    else:
        flag = False
        l1 = [False] * (25 * 27 + 1)
        for i in range(len(word) - 1):
            a = word[i]
            b = word[i + 1]
            s1 = ord(a) - 97
            s2 = ord(b) - 97
            l1[s1 * 26 + s2] = True
        ans = ''
        for i in range(len(l1)):
            if not l1[i]:
                ans += chr(i // 26 + 97)
                ans += chr(i % 26 + 97)
                flag = True
                break
        if flag:
            print(ans)
        else:
            flag = False
            l1 = [False] * (17575 + 1)
            for i in range(len(word) - 2):
                a = word[i]
                b = word[i + 1]
                c = word[i + 2]
                s1 = ord(a) - 97
                s2 = ord(b) - 97
                s3 = ord(c) - 97
                l1[s1 * 26 * 26 + s2 * 26 + s3] = True
            ans = ''
            for i in range(len(l1)):
                if not l1[i]:
                    ans += chr(i // (26 * 26) + 97)
                    ans += chr(i // 26 + 97)
                    ans += chr(i % 26 + 97)
                    flag = True
                    break
            if flag:
                print(ans)

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    s = input()
    alp = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    f = 0
    for j in range(26):
        if alp[j] not in s:
            print(alp[j])
            f = 1
            break
    if f == 0:
        for j in range(26):
            for k in range(26):
                ans = alp[j] + alp[k]
                if ans not in s:
                    print(ans)
                    f = 1
                    break
            if f == 1:
                break
    if f == 0:
        for j in range(26):
            for k in range(26):
                for l in range(26):
                    ans = alp[j] + alp[k] + alp[l]
                    if ans not in s:
                        print(ans)
                        f = 1
                        break
                if f == 1:
                    break
            if f == 1:
                break