def grid_search(g, p):
    """Hackerrank Problem: https://www.hackerrank.com/challenges/the-grid-search/problem

    Given a 2D array of digits or grid, try to find the occurrence of a given 2D pattern of digits. For example:

    Grid
    ----------
    1234567890
    0987654321
    1111111111
    1111111111
    2222222222

    Pattern
    ------
    876543
    111111
    111111

    The 2D pattern begins at the second row and the third column of the grid. The pattern is said to be present in the
    grid.

    Args:
        g (list): The grid to search, an array of strings
        p (list): The pattern to search for, an array of strings

    Returns:
        str: "YES" or "NO" depending on whether the pattern is found or not
    """
    for i in range(len(g)-len(p)+1):
        # If we find a match for the first line, store the indices to search for
        match = [x for x in range(len(g[i])) if g[i].startswith(p[0], x)]
        if match:
            # Iterate through the list of indices where the first line of the pattern matched
            for j in match:
                found = True
                # Now see if the rest of the pattern matches within the same column / index
                for k in range(1, len(p)):
                    if p[k] == g[i+k][j:j+len(p[k])]:
                        continue
                    else:
                        found = False
                        break
                if found:
                    return "YES"
    return "NO"