Gift Carpet (Codeforces 1862A)
Tags: Greedy, strings

Recently, Tema and Vika celebrated Family Day. Their friend Arina gave them a carpet, which can be represented as an $$$n \cdot m$$$ table of lowercase Latin letters.Vika hasn't seen the gift yet, but Tema knows what kind of carpets she likes. Vika will like the carpet if she can read her name on. She reads column by column from left to right and chooses one or zero letters from current column.Formally, the girl will like the carpet if it is possible to select four distinct columns in order from left to right such that the first column contains "v", the second one contains "i", the third one contains "k", and the fourth one contains "a".Help Tema understand in advance whether Vika will like Arina's gift.

Input: Each test consists of multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then follows the description of the test cases.The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \le n, m \le 20$$$) — the sizes of the carpet.The next $$$n$$$ lines contain $$$m$$$ lowercase Latin letters each, describing the given carpet.

Output: For each set of input data, output "YES" if Vika will like the carpet, otherwise output "NO".You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.

Note: In the first sample, Vika can read her name from left to right.In the second sample, Vika cannot read the character "v", so she will not like the carpet.

Sample Inputs:
5
1 4
vika
3 3
bad
car
pet
4 4
vvvv
iiii
kkkk
aaaa
4 4
vkak
iiai
avvk
viaa
4 7
vbickda
vbickda
vbickda
vbickda

Sample Outputs:
YES
NO
YES
NO
YES

Solution (Python):
t = int(input())
for i in range(t):
    (n, m) = map(int, input().split())
    mat = []
    for i in range(n):
        s = input()
        mat.append(s)
    check = 'vika'
    ind = 0

    def find_vika(ind, m, n, mat):
        for i in range(m):
            for j in range(n):
                if mat[j][i] == check[ind]:
                    ind += 1
                    if ind == len(check):
                        print('YES')
                        return
                    break
        print('NO')
    find_vika(ind, m, n, mat)

Solution (Python):
n = int(input())
for i in range(n):
    (m, n) = map(int, input().split())
    carpet = [input() for i in range(m)]
    name = 'vika'
    search = 0
    i = 0
    while i < n:
        j = 0
        while j < m:
            if carpet[j][i] == name[search]:
                search += 1
                j = m
            j += 1
        if search == 4:
            i = n
        i += 1
    if search == 4:
        print('YES')
    else:
        print('NO')

Solution (Python):
t = int(input())
for i in range(t):
    (m, n) = input().split()
    (l, c, f) = ([], 0, 0)
    out = 'vika'
    for j in range(int(m)):
        l.append(input())
    for k in range(int(n)):
        for o in range(int(m)):
            if l[o][k] == out[c]:
                c += 1
                break
        if c == 4:
            print('YES')
            f = 1
            break
    if f == 0:
        print('NO')