-
-
Save HouLinwei/b984392cc55df68c5fb366a6a76e88fd to your computer and use it in GitHub Desktop.
Python KMP algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class KMP: | |
| def partial(self, pattern): | |
| """ Calculate partial match table: String -> [Int]""" | |
| ret = [0] | |
| for i in range(1, len(pattern)): | |
| j = ret[i - 1] | |
| while j > 0 and pattern[j] != pattern[i]: | |
| j = ret[j - 1] | |
| ret.append(j + 1 if pattern[j] == pattern[i] else j) | |
| return ret | |
| def search(self, T, P): | |
| """ | |
| KMP search main algorithm: String -> String -> [Int] | |
| Return all the matching position of pattern string P in S | |
| """ | |
| partial, ret, j = self.partial(P), [], 0 | |
| for i in range(len(T)): | |
| while j > 0 and T[i] != P[j]: | |
| j = partial[j - 1] | |
| if T[i] == P[j]: j += 1 | |
| if j == len(P): | |
| ret.append(i - (j - 1)) | |
| j = 0 | |
| return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment