1796B - One and Two - 800

Updated: 2023-09-01
1 min read

On This Page

Asterisk-Minor Template (implementation, strings, 1000)

  1. If the first characters of both strings are the same, create a template that consists of the common character followed by an asterisk (e.g., a*).
  2. If the last characters of both strings are the same, create a template that consists of an asterisk followed by the common character (e.g., *b).
  3. If neither the first nor the last characters are the same, iterate through string a and check for any 2-character substring that also appears in string b.
    1. If a match is found, create a template that consists of an asterisk, the 2-character substring, and another asterisk (e.g., ab).
  4. If no template is found, print NO.

Solution

def solve():
    a = input()
    b = input()

    if a[0] == b[0]:
        print('YES')
        print(f'{a[0]}*')
    elif a[-1] == b[-1]:
        print('YES')
        print(f'*{a[-1]}')
    else:
        for i in range(len(a)-1):
            if a[i:i+2] in b:
                print('YES')
                print(f'*{a[i:i+2]}*')
                return
        print('NO')

for _ in range(int(input())):
    solve()