Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

def is _ valid _ substring ( substring , s 2 ) : return substring in s 2 or substring [ :: - 1 ]

def is_valid_substring(substring, s2):
return substring in s2 or substring[::-1] in s2
def find_substrings(s1, s2):
substrings =[]
i =0
j =0
while i < len(s1):
found = False
# Try to find the longest valid substring starting from i
for j in range(len(s1), i,-1):
substring = s1[i:j]
if is_valid_substring(substring, s2):
substrings.append(substring)
i = j
found = True
break
# If no valid substring is found, it's impossible
if not found:
return ["Impossible"]
return substrings
# Input
s1= input().strip()
s2= input().strip()
# Find and print the substrings
result = find_substrings(s1, s2)
print("|".join(result))
The time limit is exceeded it is showing when running the program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases And Python Programming MySQL MongoDB OOP And Tkinter

Authors: R. PANNEERSELVAM

1st Edition

9357011331, 978-9357011334

More Books

Students also viewed these Databases questions

Question

4. How would you deal with the store manager?

Answered: 1 week ago