There was an error while loading. Please reload this page.
1 parent db01fc3 commit d0370dbCopy full SHA for d0370db
1 file changed
strings/z_function.py
@@ -68,20 +68,15 @@ def find_pattern(pattern: str, input_str: str) -> int:
68
4
69
>>> find_pattern("xz", "zxxzxxz")
70
2
71
+ >>> find_pattern("aa", "a")
72
+ 0
73
"""
- answer = 0
74
+ pattern_length = len(pattern)
75
# concatenate 'pattern' and 'input_str' and call z_function
76
# with concatenated string
77
z_result = z_function(pattern + input_str)
78
- for val in z_result:
- # 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
+ return sum(value >= pattern_length for value in z_result[pattern_length:])
85
86
87
if __name__ == "__main__":
0 commit comments