FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-algorithms/algorithms/strings/contain_string.py at master · ilyvsc/python-algorithms · GitHub
ilyvsc
/
python-algorithms
Public
forked from
keon/algorithms
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-algorithms
/
algorithms
/
strings
/
contain_string.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
25 lines (22 loc) · 651 Bytes
Breadcrumbs
python-algorithms
/
algorithms
/
strings
/
contain_string.py
Copy path
File metadata and controls
25 lines (22 loc) · 651 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = "hello", needle = "ll"
Output: 2
Example 2:
Input: haystack = "aaaaa", needle = "bba"
Output: -1
Reference: https://leetcode.com/problems/implement-strstr/description/
"""
def
contain_string
(
haystack
,
needle
):
if
len
(
needle
)
==
0
:
return
0
if
len
(
needle
)
>
len
(
haystack
):
return
-
1
for
i
in
range
(
len
(
haystack
)):
if
len
(
haystack
)
-
i
<
len
(
needle
):
return
-
1
if
haystack
[
i
:
i
+
len
(
needle
)]
==
needle
:
return
i
return
-
1
Back
|
FazBrowse Home
|
New Git URL