1796B - One and Two - 800
On This Page
Asterisk-Minor Template (implementation, strings, 1000)
- 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*
). - 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
). - 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 stringb
.- If a match is found, create a template that consists of an asterisk, the 2-character substring, and another asterisk (e.g.,
ab
).
- If a match is found, create a template that consists of an asterisk, the 2-character substring, and another asterisk (e.g.,
- 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()