Skip to content

Commit 42fc750

Browse files
new question added
1 parent ac7b26b commit 42fc750

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.vscode/
22
__pycache__/
3-
.DS_Store
3+
.DS_Store
4+
14. Questions/code.py
5+
14. Questions/temp.py
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Implement strStr().
3+
4+
Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
5+
6+
Clarification:
7+
8+
What should we return when needle is an empty string? This is a great question to ask during an interview.
9+
10+
For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().
11+
12+
Question: https://leetcode.com/problems/implement-strstr/
13+
14+
"""
15+
16+
class Solution:
17+
def strStr(self, haystack: str, needle: str) -> int:
18+
if not needle:
19+
return 0
20+
21+
for i in range(len(haystack)):
22+
if haystack[i:i+len(needle)] == needle:
23+
return i
24+
return -1

0 commit comments

Comments
 (0)