Created
September 12, 2022 16:05
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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