Answer to Question #330771 in Python for Sind

Question #330771

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





1
Expert's answer
2022-04-19T08:07:07-0400
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)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS