Question: program in python using only recursion I need to check if s3 is a complete compinantion (interleaved) of s1 and s2. for example : s1

program in python using only recursion

I need to check if s3 is a complete compinantion (interleaved) of s1 and s2. for example : s1 = "abc" , s2 = "def" . s3 can be "abcdef" , "adbecf" the order of the characters in s1 and s2 must be the same in s3 (but not just one after another)

if you must use built in functions you can use only - index , find , split , [ : ], s1

the function must get (s1,s2,s3) and return True or False (only using recursion even If you need to define another function to help it must be using recursion )

#

S1

S2

S3

output

1

""

""

""

True

2

"abc"

""

"abc"

True

3

"abc"

"def"

"abcdef"

True

4

"abc"

"def"

"daebcf"

True

5

"abc"

"def"

"adfbce"

False

6

"ab c"

"def"

"deab cf"

True

7

"morning Venm"

"Good ita!"

"Good morning Vietnam!"

True

8

"ABZ"

"ABAXZ"

"ABAXABZZ"

True

9

"ABAXZ"

"ABZ"

"ABAXABZZ"

True

10

"12189"

"129"

"12181299"

True

11

"12189"

"129"

"12112998"

False

12

"TTTT"

"X"

"XXXXX"

False

I wrote this one: (the problem when I run number 8 it gives me False , the reasone is because s1 and s2 start with the same letter and s1 is shorter , so 9 is working because s1 is the longest , I need an efficient solution)

def check(s1,s2,s3): l1, l2, l3 = len(s1), len(s2),len(s3) if (l1 == l2 == l3 == 0): return True if (l1 == 0) and (s2 == s3 or s1 == s3): return True if (l3 != (l1 +l2)): #or (l3 == 0 and (l1 > 0 or l2 > 0)): return False if s1 and s1[0] == s3[0]: return True and organized_comp(s1[1:], s2, s3[1:]) if s2 and s2[0] == s3[0]: return True and organized_comp(s1, s2[1:], s3[1:])

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!