Build a brute force approach in Python for the following problem.
Given two sequences of vowels, find the length of longest subsequence present in both of them.
NOTE: A subsequence is a sequence that appears in the same order, but not necessarily contiguous. For example, “aei”, “aeo”, “eio”, “aiu”, ... are subsequences of “aeiou”.
Sample Input 1:
aeiou aiu
Sample Output 1:
3
Sample Input 2:
uieiai uueuouiaua
Sample Output 2:
4
Sample Input 3:
uuuuouueea euooooe
Sample Output 3:
3
Sample Input 4:
aeeeuu ieoouu
Sample Output 4:
3
Sample Input 5:
ieiaeoeee oaoai
Sample Output 5:
2
Sample Input 6:
oiaioieie eoiuueaea
Sample Output 6:
4
Sample Input 7:
uoaeuou ioooo
Sample Output 7:
2
Sample Input 8:
uaoaoaao iaeeo
Sample Output 8:
2
Sample Input 9:
uaieiaiae ieoueoiai
Sample Output 9:
5
Sample Input 10:
eaueaiu ioaaoueee
Sample Output 10:
3
test using stdin → stdout
Code
'''using bruteforce it can be solved by both recursive or iterative method
Here,recursion method has been used'''
def res(b,c,d,e):
if d==0 or e==0: #empty string
return 0
if b[d-1]==c[e-1]:
return 1 + res(b,c,d-1,e-1)
else:
return max(res(b,c,d, e-1), res(b,c,d-1, e))
a=input() #get the input
b,c=a.split(" ")
d=len(b) # length of first input
e=len(c) #length of second input
v=res(b,c,d,e) #call the function and store it in variable v
print(v) # print the result
Terminal Work
.
Get Answers For Free
Most questions answered within 1 hours.