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

后端 未结 4 1154
南旧
南旧 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:35

    As @sergey mentioned earlier, there is no need to do backtracking in this case. Here just another Python version to the problem:

    >>> A = [1, 2, 3]
    >>> B = [5, 6, 1, 7, 8, 2, 4, 3]
    >>> def is_subsequence(A, B):
        it = iter(B)
        return all(x in it for x in A)
    
    >>> is_subsequence(A, B)
    True
    >>> is_subsequence([1, 3, 4], B)
    False
    >>> 
    

提交回复
热议问题