-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path28_ImplementstrStr.py
More file actions
40 lines (31 loc) · 888 Bytes
/
28_ImplementstrStr.py
File metadata and controls
40 lines (31 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from typing import List
class Solution:
def strStr1(self, haystack: str, needle: str) -> int:
return haystack.find(needle)
def strStr(self, haystack: str, needle: str) -> int:
if len(needle) == 0:
return 0
for i in range(len(haystack)):
if haystack[i:i+len(needle)] == needle:
return i
return -1
def main():
# Example:1
haystack = "hello"
needle = "ll"
print(Solution().strStr(haystack, needle))
# Input: haystack = "hello", needle = "ll"
# Output: 2
# Example:2
haystack = "aaaaa"
needle = "bba"
print(Solution().strStr(haystack, needle))
# Input:haystack = "aaaaa", needle = "bba"
# Output: -1
# Error case
haystack = "a"
needle = "a"
print(Solution().strStr(haystack, needle))
# Output: -1
if __name__ == '__main__':
main()