Skip to content

Commit 671e570

Browse files
nikalosagithub-actionscclauss
authored
Implement prefix function, knuth-morris-pratt another usage (#2099)
* Implement prefix function, knuth-morris-pratt another usage * fixup! Format Python code with psf/black push * Fix style * updating DIRECTORY.md * Update prefix_function.py Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent fb3a228 commit 671e570

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@
650650
* [Manacher](https://github.com/TheAlgorithms/Python/blob/master/strings/manacher.py)
651651
* [Min Cost String Conversion](https://github.com/TheAlgorithms/Python/blob/master/strings/min_cost_string_conversion.py)
652652
* [Naive String Search](https://github.com/TheAlgorithms/Python/blob/master/strings/naive_string_search.py)
653+
* [Prefix Function](https://github.com/TheAlgorithms/Python/blob/master/strings/prefix_function.py)
653654
* [Rabin Karp](https://github.com/TheAlgorithms/Python/blob/master/strings/rabin_karp.py)
654655
* [Remove Duplicate](https://github.com/TheAlgorithms/Python/blob/master/strings/remove_duplicate.py)
655656
* [Reverse Words](https://github.com/TheAlgorithms/Python/blob/master/strings/reverse_words.py)

strings/prefix_function.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
https://cp-algorithms.com/string/prefix-function.html
3+
4+
Prefix function Knuth–Morris–Pratt algorithm
5+
6+
Different algorithm than Knuth-Morris-Pratt pattern finding
7+
8+
E.x. Finding longest prefix which is also suffix
9+
10+
Time Complexity: O(n) - where n is the length of the string
11+
"""
12+
13+
14+
def prefix_function(input_string: str) -> list:
15+
"""
16+
For the given string this function computes value for each index(i),
17+
which represents the longest coincidence of prefix and sufix
18+
for given substring (input_str[0...i])
19+
20+
For the value of the first element the algorithm always returns 0
21+
22+
>>> prefix_function("aabcdaabc")
23+
[0, 1, 0, 0, 0, 1, 2, 3, 4]
24+
>>> prefix_function("asdasdad")
25+
[0, 0, 0, 1, 2, 3, 4, 0]
26+
"""
27+
28+
# list for the result values
29+
prefix_result = [0] * len(input_string)
30+
31+
for i in range(1, len(input_string)):
32+
33+
# use last results for better performance - dynamic programming
34+
j = prefix_result[i - 1]
35+
while j > 0 and input_string[i] != input_string[j]:
36+
j = prefix_result[j - 1]
37+
38+
if input_string[i] == input_string[j]:
39+
j += 1
40+
prefix_result[i] = j
41+
42+
return prefix_result
43+
44+
45+
def longest_prefix(input_str: str) -> int:
46+
"""
47+
Prefix-function use case
48+
Finding longest prefix which is sufix as well
49+
50+
>>> longest_prefix("aabcdaabc")
51+
4
52+
>>> longest_prefix("asdasdad")
53+
4
54+
>>> longest_prefix("abcab")
55+
2
56+
"""
57+
58+
# just returning maximum value of the array gives us answer
59+
return max(prefix_function(input_str))
60+
61+
62+
if __name__ == "__main__":
63+
import doctest
64+
65+
doctest.testmod()

0 commit comments

Comments
 (0)