Ugly Pairs (Codeforces 1156B)
Tags: Greedy, dfs and similar

You are given a string, consisting of lowercase Latin letters.A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $$$(1, 2)$$$ — "ab" and $$$(2, 3)$$$ — "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet.Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same.If there are multiple answers, print any of them.You also have to answer $$$T$$$ separate queries.

Input: The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of queries.Each of the next $$$T$$$ lines contains string $$$s$$$ $$$(1 \le |s| \le 100)$$$ — the string for the next query. It is guaranteed that it contains only lowercase Latin letters.Note that in hacks you have to set $$$T = 1$$$.

Output: Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th query.If the answer for the $$$i$$$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same.If there are multiple answers, print any of them.Otherwise print "No answer" for that query.

Note: In the first example answer "bdac" is also correct.The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok.There are lots of valid answers for the third example.

Sample Inputs:
4
abcd
gg
codeforces
abaca

Sample Outputs:
cadb
gg
codfoerces
No answer

Solution (Python):
def check(a, b):
    return not a or not b or abs(ord(a[-1]) - ord(b[0])) > 1
for _ in range(int(input())):
    s = input()
    odds = list(sorted(filter(lambda c: (ord(c) - ord('a')) % 2 == 1, s)))
    evens = list(sorted(filter(lambda c: (ord(c) - ord('a')) % 2 == 0, s)))
    if check(odds, evens):
        print(''.join(odds + evens))
    elif check(evens, odds):
        print(''.join(evens + odds))
    else:
        print('No answer')

Solution (Python):
from collections import defaultdict, Counter
for _ in range(int(input())):
    s = input()
    f = True
    for i in range(len(s) - 1):
        if abs(ord(s[i]) - ord(s[i + 1])) == 1:
            f = False
            break
    if f:
        print(s)
        continue
    mp1 = Counter(s)
    mp2 = defaultdict(chr)
    mp3 = defaultdict(int)
    l1 = []
    l2 = []
    l3 = []
    ans = ''
    j = ord('a')
    for i in range(1, 27):
        mp2[chr(j)] = i
        j += 1
    j = ord('a')
    for i in range(1, 27):
        mp3[i] = chr(j)
        j += 1
    for i in s:
        if mp2[i] not in l1:
            l1.append(mp2[i])
    l1.sort(reverse=True)
    for i in range(1, len(l1), 2):
        l2.append(l1[i])
    for i in range(0, len(l1), 2):
        l2.append(l1[i])
        l3.append(l1[i])
    for i in range(1, len(l1), 2):
        l3.append(l1[i])
    for i in range(len(l2) - 1):
        if abs(l2[i] - l2[i + 1]) == 1:
            f = not f
            break
    if f:
        for i in range(len(l2) - 1):
            if abs(l3[i] - l3[i + 1]) == 1:
                f = not f
                break
        if not f:
            print('No answer')
            continue
        for i in l3:
            ans += mp3[i] * mp1[mp3[i]]
        print(ans)
        continue
    for i in l2:
        ans += mp3[i] * mp1[mp3[i]]
    print(ans)

Solution (Python):
for i in range(int(input())):
    alp = 'abcdefghijklmnopqrstuvwxyz'
    s = str(input())
    x = sorted(s)
    s = ''
    for i in x:
        s += i
    odd = ''
    even = ''
    for i in s:
        index = alp.index(i)
        index += 1
        if index % 2 == 0:
            even += i
        else:
            odd += i
    a = odd + even
    b = even + odd
    ac = 0
    good_a = 0
    while ac != len(a) - 1:
        index1 = alp.index(a[ac])
        index1 += 1
        index2 = alp.index(a[ac + 1])
        index2 += 1
        if index2 - index1 != 1 and index2 - index1 != -1:
            good_a += 1
        ac += 1
    if good_a == len(a) - 1:
        print(a)
    else:
        bc = 0
        good_b = 0
        while bc != len(b) - 1:
            index1 = alp.index(b[bc])
            index1 += 1
            index2 = alp.index(b[bc + 1])
            index2 += 1
            if index2 - index1 != 1 and index2 - index1 != -1:
                good_b += 1
            bc += 1
        if good_b == len(b) - 1:
            print(b)
        else:
            print('No answer')