Answer to Question #336261 in Python for sandhya

Question #336261

substring:

you are given two strings N &K your goal is to determine the smallest substring of N that contains all the characters in K If no substring present in N print No matches found

note: if a character is repeated multiple time in K your substring should also contain that character repeated the same no.of times

i/p:the 1st line input two strings N&K

input:

stealen lent

o/p:

tealen

i/p:

tomato tomatho

no matches found



1
Expert's answer
2022-05-03T14:19:16-0400
def find_smallest_str( s1, s2):
	for i in range(1,len(s1)+1):
		for j in range(len(s1)-i+1):
			all_in = True
			for char in s2:
				if char not in s1[j:j+i]:
					all_in = False
					break
			if all_in:
				print(s1[j:j+i])
				return
	print('no matches found')






s1, s2 = input().split()
find_smallest_str( s1, s2)

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