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 with AI-Powered Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions