Question

I understand my question is similar to a couple posts out there but I think it has aspects that make it different. I am looking to find a sub-array or pattern within a much larger array. I will be working with arrays with thousands even millions of rows and I need to find a pattern within that array. The values I will be searching for are similar to the values in the array. For instance my array of say 10,000 rows will be full of 1's 0's L's and H's mainly and I will be searching for a certain pattern in there for example looking for 1 0 1 1 H.

From what I can see most of the solutions posted on other posts are dealing with much smaller scale arrays and where the sub array is more distinct from the source array. Also, when I find the array within the source array I need to return the location of that sub-array. (I am looking to do this code in C#)

Was it helpful?

Solution

This is fundamentally the same as a substring search. They are both about finding subsequences inside a random-access larger sequence. And from your description, it sounds like your array is an array of chars, which is exactly what a string is.

The algorithm you describe in your note is pretty good and easy to code correctly. If it's not fast enough, look at KMP.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top