-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Day 14: Find 2D pattern in matrix #9
Conversation
atm the code is not finished, today was a long day and I couldn't bring myself to finish the code. But the thougth process is all there and it's not too complex. |
|
||
there are only 4 possible starting locations: `(0, 0), (0, 1), (1,0), (1,1)`. | ||
|
||
I don't think it's possible to do this problem and faster. So it's not |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: I haven't seen or done this problem before, so this might be totally wrong.
This problem reminds me a lot of fast string matching, which means that there's probably a faster way to do it. In KMP and other linear string search algorithms, a key observation is that you might be able to skip steps. For example,
needle = 'abcd'
haystack = 'abcfffffabcdffffff'
When we compare by eye, we notice that the first three characters of needle
and haystack
match, but the fourth doesn't. Subsequently, we skip ahead when looking, because we know that the fourth character was an f
, but needle[0] != 'f'
, so we can jump a few characters ahead. Specifically, we can start searching again from haystack[4:]
.
I suspect something similar can work in this problem, which would give it ultimately a runtime of O(nm)
, where n
and m
are the dimensions of the big matrix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
damn uR RIGHT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this article is helping me a lot to understand KMP: http://jakeboxer.com/blog/2009/12/13/the-knuth-morris-pratt-algorithm-in-my-own-words/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't done thorough research yet, but it seems like this one also generalizes well to two-dimensional patern matching https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm
plz comment with thoughts, criticisms, anything! :)