Spy-string (Codeforces 1360F)
Tags: Others, hashing

You are given $$$n$$$ strings $$$a_1, a_2, \ldots, a_n$$$: all of them have the same length $$$m$$$. The strings consist of lowercase English letters.Find any string $$$s$$$ of length $$$m$$$ such that each of the given $$$n$$$ strings differs from $$$s$$$ in at most one position. Formally, for each given string $$$a_i$$$, there is no more than one position $$$j$$$ such that $$$a_i[j] \ne s[j]$$$.Note that the desired string $$$s$$$ may be equal to one of the given strings $$$a_i$$$, or it may differ from all the given strings.For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first.

Input: The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow.Each test case starts with a line containing two positive integers $$$n$$$ ($$$1 \le n \le 10$$$) and $$$m$$$ ($$$1 \le m \le 10$$$) — the number of strings and their length.Then follow $$$n$$$ strings $$$a_i$$$, one per line. Each of them has length $$$m$$$ and consists of lowercase English letters.

Output: Print $$$t$$$ answers to the test cases. Each answer (if it exists) is a string of length $$$m$$$ consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print "-1" ("minus one", without quotes).

Note: The first test case was explained in the statement.In the second test case, the answer does not exist.

Sample Inputs:
5
2 4
abac
zbab
2 4
aaaa
bbbb
3 3
baa
aaa
aab
2 2
ab
bb
3 1
a
b
c

Sample Outputs:
abab
-1
aaa
ab
z

Solution (Python):
import sys
caseNumber = int(sys.stdin.readline())
for i in range(caseNumber):
    (stringNumber, stringLength) = list(map(int, sys.stdin.readline().split()))
    strings = []
    for j in range(stringNumber):
        strings.append(sys.stdin.readline().strip())
    counter = 1
    possible = []
    while counter < stringNumber:
        differences = []
        for j in range(stringLength):
            if strings[0][j] != strings[counter][j]:
                differences.append(j)
        if len(differences) > 1:
            possible = []
            possible.append(strings[0][:differences[0] + 1] + strings[counter][differences[0] + 1:])
            possible.append(strings[counter][:differences[0] + 1] + strings[0][differences[0] + 1:])
            break
        counter += 1
    if len(possible) == 0:
        print(strings[0])
    else:
        counter1 = 0
        counter2 = 0
        while counter1 != 2:
            good = True
            counter2 = 0
            while counter2 != stringNumber:
                nDifferences = 0
                for j in range(stringLength):
                    if possible[counter1][j] != strings[counter2][j]:
                        nDifferences += 1
                if nDifferences > 1:
                    good = False
                    break
                counter2 += 1
            if good:
                print(possible[counter1])
                break
            counter1 += 1
        if not good:
            print(-1)

Solution (Python):
T = int(input())
for t in range(T):
    (n, m) = [int(i) for i in input().split()]
    l = []
    for i in range(n):
        s = input()
        l.append(s)
    flag = 1
    count = 0
    for i in range(1, n):
        count = 0
        for j in range(m):
            if l[i][j] != l[0][j]:
                count += 1
        if count > 1:
            flag = 0
            break
    if flag == 1:
        print(l[0])
    else:
        index = 0
        while index < m and flag == 0:
            flag = 1
            s = list(l[0])
            temp = []
            for i in range(n):
                count = 0
                for j in range(m):
                    if j != index:
                        if l[i][j] != l[0][j]:
                            count += 1
                            ind = j
                if count == 1:
                    temp.append(l[i])
                elif count > 1:
                    flag = 0
                    break
            if flag == 1:
                ex = temp[0][index]
                for i in range(len(temp)):
                    if temp[i][index] != ex:
                        flag = 0
                        break
                if flag == 1:
                    s[index] = ex
                    x = ''
                    for i in range(m):
                        x += s[i]
                    print(x)
            index += 1
        if flag == 0:
            print(-1)

Solution (Python):
def solve():
    (n, m) = map(int, input().split())
    a = [input() for _ in range(n)]
    found = False
    for i in range(m):
        for c in range(26):
            sol = list(a[0])
            sol[i] = chr(c + ord('a'))
            sol = ''.join(sol)
            found = True
            for j in range(n):
                d = 0
                for k in range(m):
                    if sol[k] != a[j][k]:
                        d += 1
                if d > 1:
                    found = False
                    break
            if found:
                break
        if found:
            break
    if found:
        print(sol)
    else:
        print(-1)
t = int(input())
for _ in range(t):
    solve()