-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dynamic Programming-Abbreviation.py
54 lines (40 loc) · 1.17 KB
/
Dynamic Programming-Abbreviation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#Problem Link : https://www.hackerrank.com/challenges/abbr/problem?isFullScreen=true&h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=dynamic-programming
#Ans:
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'abbreviation' function below.
#
# The function is expected to return a STRING.
# The function accepts following parameters:
# 1. STRING a
# 2. STRING b
#
def solve():
a = input()
b = input()
n, m = len(a), len(b)
dp = [[False] * (m + 1) for _ in range(n + 1)]
dp[0][0] = True
for i in range(1, n + 1):
dp[i][0] = a[:i].islower()
for i in range(1, n + 1):
for j in range(1, m + 1):
if a[i - 1].isupper():
dp[i][j] = dp[i - 1][j - 1] if a[i - 1] == b[j - 1] else False
else:
if a[i - 1].upper() == b[j - 1]:
dp[i][j] = dp[i - 1][j] or dp[i - 1][j - 1]
else:
dp[i][j] = dp[i - 1][j]
return 'YES' if dp[n][m] else 'NO'
def main():
queries = int(input())
for _ in range(queries):
print(solve())
if __name__ == '__main__':
main()