Given two strings N and K. Your goal is to determine the smallest substring of N that contains all the characters in K. If no substring is present in N print no matches found.
Note: If character is repeated multiple times in K, your substring should also contain that character repeated same number of times.
Input: first line contains two strings N and K
Output: string representing the smallest substring as mentioned above
Input1: stealen lent
Output 1: tealen
Input 2: tomato tomatho
Output 2: No matches found
def all_in_sub(s1,s2):
for el in s2:
if el not in s1:
return False
return True
def find_substr(n, k):
min_sub_str = False
for i in range(len(n)-1):
for j in range(i+1,len(n)+1):
if all_in_sub(n[i:j], k):
if not min_sub_str or len(min_sub_str) > len(n[i:j]):
min_sub_str = n[i:j]
if min_sub_str:
print(min_sub_str)
else:
print('No matches found')
n, k = input().split()
find_substr(n, k)
Comments
Leave a comment