Skip to content

Instantly share code, notes, and snippets.

@Robotboy93
Created September 12, 2022 16:05
Show Gist options
  • Save Robotboy93/5825f0eb48d1b1704543ab78f1395cc6 to your computer and use it in GitHub Desktop.
Save Robotboy93/5825f0eb48d1b1704543ab78f1395cc6 to your computer and use it in GitHub Desktop.
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.
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
x = [a for a in haystack]
y = [b for b in needle]
k = -1
if len(y) <= len(x):
for i in range(len(x)-len(y)+1):
if x[i] == y[0]:
for j in range(len(y)):
if x[i+j] == y[j]:
k = i
else:
k = -1
break
if k!= -1:
break
return k
pass
ret = Solution().strStr("apple","sdddd")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment