MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / find_pattern

Function find_pattern

strings/z_function.py:59–84  ·  view source on GitHub ↗

Example of using z-function for pattern occurrence Given function returns the number of times 'pattern' appears in 'input_str' as a substring >>> find_pattern("abr", "abracadabra") 2 >>> find_pattern("a", "aaaa") 4 >>> find_pattern("xz", "zxxzxxz") 2

(pattern: str, input_str: str)

Source from the content-addressed store, hash-verified

57
58
59def find_pattern(pattern: str, input_str: str) -> int:
60 """
61 Example of using z-function for pattern occurrence
62 Given function returns the number of times 'pattern'
63 appears in 'input_str' as a substring
64
65 >>> find_pattern("abr", "abracadabra")
66 2
67 >>> find_pattern("a", "aaaa")
68 4
69 >>> find_pattern("xz", "zxxzxxz")
70 2
71 """
72 answer = 0
73 # concatenate 'pattern' and 'input_str' and call z_function
74 # with concatenated string
75 z_result = z_function(pattern + input_str)
76
77 for val in z_result:
78 # if value is greater then length of the pattern string
79 # that means this index is starting position of substring
80 # which is equal to pattern string
81 if val >= len(pattern):
82 answer += 1
83
84 return answer
85
86
87if __name__ == "__main__":

Callers

nothing calls this directly

Calls 1

z_functionFunction · 0.85

Tested by

no test coverage detected