Question

Give an example of a dynamic programming algorithm which solves the following problem: Input. Two strings...

Give an example of a dynamic programming algorithm which solves the following problem:

Input. Two strings w1 and w2
Output. Yes if w1 contains all the letters of w2 in order, no otherwise.
(example problem instance and solutions)
Input. w1="abccba", w2="bba"
Output. Yes
Input. w1="abcabc", w2="bba"
Output. No

You do not need to prove the algorithm is correct. But you should analyze its runtime.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The algorithm is given as:

function boolean contains(String str1, String str2) {

m = str1.length()

n = str2.length()

j = 0

for i = 0 to i < m such that j < n do

if str1[i] == str2[j] then

j++

end if

end loop

if (j == n) then

print "Yes"

else

print "No"

end if

}

Time complexity: O(m) where m is the length of str1.

Add a comment
Know the answer?
Add Answer to:
Give an example of a dynamic programming algorithm which solves the following problem: Input. Two strings...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT