Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!