📌  相关文章
📜  检查一个数组是否是另一个数组的子数组

📅  最后修改于: 2022-05-13 01:57:49.194000             🧑  作者: Mango

检查一个数组是否是另一个数组的子数组

给定两个数组 A[] 和 B[],由n m 整数。任务是检查数组 B[] 是否是数组 A[] 的子数组。
例子

资料来源:签证面试经验
简单方法:一种简单的方法是运行两个嵌套循环并生成数组 A[] 的所有子数组,然后再使用一个循环检查 A[] 的任何子数组是否等于数组 B[]。
高效方法:一种有效的方法是使用两个指针同时遍历两个数组。保持数组 B[] 的指针不变,如果 A[] 的任何元素与 B[] 的第一个元素匹配,则增加两个数组的指针,否则将 A 的指针设置为前一个起点的下一个元素,并且将 B 的指针重置为 0。如果 B 的所有元素都匹配,则打印 YES,否则打印 NO。
下面是上述方法的实现:

C++
// C++ program to check if an array is
// subarray of another array
 
#include 
using namespace std;
 
// Function to check if an array is
// subarray of another array
bool isSubArray(int A[], int B[], int n, int m)
{
    // Two pointers to traverse the arrays
    int i = 0, j = 0;
 
    // Traverse both arrays simultaneously
    while (i < n && j < m) {
 
        // If element matches
        // increment both pointers
        if (A[i] == B[j]) {
 
            i++;
            j++;
 
            // If array B is completely
            // traversed
            if (j == m)
                return true;
        }
        // If not,
        // increment i and reset j
        else {
            i = i - j + 1;
            j = 0;
        }
    }
 
    return false;
}
 
// Driver Code
int main()
{
    int A[] = { 2, 3, 0, 5, 1, 1, 2 };
    int n = sizeof(A) / sizeof(int);
    int B[] = { 3, 0, 5, 1 };
    int m = sizeof(B) / sizeof(int);
 
    if (isSubArray(A, B, n, m))
        cout << "YES\n";
    else
        cout << "NO\n";
 
    return 0;
}


Java
// Java program to check if an array is
// subarray of another array
class gfg
{
     
    // Function to check if an array is
    // subarray of another array
    static boolean isSubArray(int A[], int B[],
                                   int n, int m)
    {
        // Two pointers to traverse the arrays
        int i = 0, j = 0;
     
        // Traverse both arrays simultaneously
        while (i < n && j < m)
        {
     
            // If element matches
            // increment both pointers
            if (A[i] == B[j])
            {
     
                i++;
                j++;
     
                // If array B is completely
                // traversed
                if (j == m)
                    return true;
            }
             
            // If not,
            // increment i and reset j
            else
            {
                i = i - j + 1;
                j = 0;
            }
        }
        return false;
    }
     
    // Driver Code
    public static void main(String arr[])
    {
        int A[] = { 2, 3, 0, 5, 1, 1, 2 };
        int n = A.length;
        int B[] = { 3, 0, 5, 1 };
        int m = B.length;
     
        if (isSubArray(A, B, n, m))
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}
 
// This code is contributed by gp6


Python3
# Python3 program to check if an array is
# subarray of another array
 
# Function to check if an array is
# subarray of another array
def isSubArray(A, B, n, m):
     
    # Two pointers to traverse the arrays
    i = 0; j = 0;
 
    # Traverse both arrays simultaneously
    while (i < n and j < m):
 
        # If element matches
        # increment both pointers
        if (A[i] == B[j]):
 
            i += 1;
            j += 1;
 
            # If array B is completely
            # traversed
            if (j == m):
                return True;
         
        # If not,
        # increment i and reset j
        else:
            i = i - j + 1;
            j = 0;
         
    return False;
 
# Driver Code
if __name__ == '__main__':
    A = [ 2, 3, 0, 5, 1, 1, 2 ];
    n = len(A);
    B = [ 3, 0, 5, 1 ];
    m = len(B);
 
    if (isSubArray(A, B, n, m)):
        print("YES");
    else:
        print("NO");
 
# This code is contributed by Rajput-Ji


C#
// C# program to check if an array is
// subarray of another array
using System;
 
public class GFG
{
      
    // Function to check if an array is
    // subarray of another array
    static bool isSubArray(int []A, int []B,
                                   int n, int m)
    {
        // Two pointers to traverse the arrays
        int i = 0, j = 0;
      
        // Traverse both arrays simultaneously
        while (i < n && j < m)
        {
      
            // If element matches
            // increment both pointers
            if (A[i] == B[j])
            {
      
                i++;
                j++;
      
                // If array B is completely
                // traversed
                if (j == m)
                    return true;
            }
              
            // If not,
            // increment i and reset j
            else
            {
                i = i - j + 1;
                j = 0;
            }
        }
        return false;
    }
      
    // Driver Code
    public static void Main(String []arr)
    {
        int []A = { 2, 3, 0, 5, 1, 1, 2 };
        int n = A.Length;
        int []B = { 3, 0, 5, 1 };
        int m = B.Length;
      
        if (isSubArray(A, B, n, m))
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
    }
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
YES