s = "BWABYXFYTTZDSGHYXEEZOOPXYZ"
t = "XYYZ"
from collections import Counter, defaultdict
def shortest_substring(st, pat):
n = len(st)
cnt = Counter(pat)
keys = cnt.keys()
dct = defaultdict(int)
i, j = 0, 0
min_len = float('inf')
min_st = ""
while j < n:
dct[st[j]] += 1
while all(dct[k] >= cnt[k] for k in keys): # Valid window found
if (j - i + 1) < min_len:
min_len = j - i + 1
min_st = st[i:j+1]
dct[st[i]] -= 1
if dct[st[i]] == 0:
del dct[st[i]] # Remove zero-count keys to keep dictionary clean
i += 1
j += 1
return min_st
r = shortest_substring(s, t)
print(r)
To embed this program on your website, copy the following code and paste it into your website's HTML: