leetCode 115: given a string S and a string T, count the number of distinct subsequences of S which equals T.
Input: S = "babgbag", T = "bag"
Output: 5
Explanation:
As shown below, there are 5 ways you can generate "bag" from S.
(The caret symbol ^ means the chosen letters)
babgbag
^^ ^
babgbag
^^ ^
babgbag
^ ^^
babgbag
^ ^^
babgbag
^^^
first thought
for each alpha in T, traversing S, find a match, return; then move forward for next alpha in T and traversing S again,
|
|
this is a failed trail. Since this kind of problem really can’t be solved by purely nested for-loop. think about, at the last level (traverse S
to get g
), there needs one for-loop, then the previous level or the level above (traverse S
to get a
) needs another loop. and each “a” needs one for-loop to traverse all possible “g” in S
again. and the top level (travers S
to get “b”) is another for-loop, and each “b” need another for-loop to traverse “a”. and this is only three levels (b->a->g)
basically if implementing tree traversing with for-loop, since tree structure has many branches to the end, since even each sub-branch needs a for-loop, there will be exponent disaster of for-loop to cover all branches.
second thoughts
what’s the different mindset? recursive. when dealing with many-branches-travsersing (e.g. a tree strucutre), recursive is nature to think. since recursive only consider the neighbor two level branches.
for this example, inside each recursive need a for-loop to catch all possible positions of the satisfied character, and stack is need to, when visiting a new character from T
need poush, and when find out a T
, then need to pop out the last character.
during breadth traversing tree, either using a pure recursive or using stack with a while-loop. but here it needs a for-loop and stack inside each recursive. (a little complex)
|
|
TODO: there suppose be a general pattern for this kind of problem.
third thoughts
if the problem is too complex to handle at first, more memory always make it looks simple, using containers to split the content. with 2 for-loops to create #num of buckets to store positions of each alphaet in T
|
|
b 0, 2, 4
a 1, 5
g 3, 6
after get the map, then play the combinations games. that’s actually still a recursive problem.