دنبال کننده ها

۱۳۹۶ مهر ۱۱, سه‌شنبه

recursion - Deleting Substrings, given 2 strings s and t, deleting t from s

[ad_1]



Given two strings s and t, if s contains t, then deleted this part, combine the rest string, if the rest string still contains t, keep deleting till no t pattern in s or s is empty string. You can delete from the first t string appear, or you can delete from the last t string appear. Find the maximum deleting time you can reach.
Example1:
s = aabb, t = ab
answer = 2, 1: a[ab]b -> ab 2: [ab]-> ""

Example2:
s = bbababa, t = bab
answer = 2, 1: b[bab]aba -> baba, 2: [bab]a -> a
wrong = 1, 1: bba[bab]a -> bbaa
So deleting from different position will generate different string.

I using recursive way and a set to solve, for loop from 0 - s.length() - t.length() + 1, if found the match then form the new string, and if the new string not in set, then add to set and call the function. If it's in set, means it already done before, so don't need to do again. My problem is it can pass some cases, but it will get time limit in other cases. Not sure how I can save more time?




[ad_2]

لینک منبع