How do you check if one array is a subsequence of another?

后端 未结 4 1153
南旧
南旧 2020-12-21 09:44

I\'m looking to explore different algorithms, both recursive and dynamic programming, that checks if one arrayA is a subsequence of arrayB. For example,

arra         


        
4条回答
  •  离开以前
    2020-12-21 10:20

    As dasblinkenlight has correctly said(and i could not have phrased it better than his answer!!) a greedy approach works absolutely fine. You could use the following pseudocode (with just a little more explanation but totally similar to what dasblinkenlight has written)which is similar to the merging of two sorted arrays.

    A = {..}
    B = {..}
    
    j = 0, k = 0
    /*j and k are variables we use to traverse the arrays A and B respectively*/
    
    for(j=0;j

    Time Complexity is O(|A|+|B|) in the worst case, where |A| & |B| are the number of elements present in Arrays A and B respectively. Thus you get a linear complexity.

提交回复
热议问题