I have a larger 2d char grid of NxN
(2 <= N <= 800)
. I am given a smaller 2d grid of KxK
( 2 <= K <= 100)
. For example, lets N = 3 and K = 2 and followings are the matrices,
Larger:
abc
abd
aaa
Smaller:
bd
aa
Problem 1: I have to return if the larger matrix contains the smaller matrix. For example above smaller matrix matched inside larger one.
Problem 2: I have to return the staring position mashing part on NxN
if found. Above example return matched and position = (1, 1)
#0 based
My assumption:
My assumption was to go with hashing. but still if there is any better idea to search efficiently. For example, I can make a hash function which will produce indices for all possible squares from NxN (2x2, 3x3, 4x4, ... , 100x100 as K can be upto 100) for all valid positions
(0,0), (0,1), ..., (0, N-K)
(1,0), (1,1), ..., (1, N-K)
. .
. .
(N-K,0), (N-K, 1) .... (N-K, N-K)
And then I can keep positions in the associated indices and when a input KxK comes I just run same hash function and see if returned index has a position or not.