From 0f8078a26dc2637a5a3ddb34a98f3eec81ee517a Mon Sep 17 00:00:00 2001 From: Deven <63876261+devenperez@users.noreply.github.com> Date: Sun, 8 Jan 2023 09:30:37 -0500 Subject: [PATCH] Time: 17 ms (82.26%), Space: 13.4 MB (65.37%) - LeetHub --- ...dex-of-the-first-occurrence-in-a-string.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 28-find-the-index-of-the-first-occurrence-in-a-string/28-find-the-index-of-the-first-occurrence-in-a-string.py diff --git a/28-find-the-index-of-the-first-occurrence-in-a-string/28-find-the-index-of-the-first-occurrence-in-a-string.py b/28-find-the-index-of-the-first-occurrence-in-a-string/28-find-the-index-of-the-first-occurrence-in-a-string.py new file mode 100644 index 0000000..cf42492 --- /dev/null +++ b/28-find-the-index-of-the-first-occurrence-in-a-string/28-find-the-index-of-the-first-occurrence-in-a-string.py @@ -0,0 +1,19 @@ +class Solution(object): + def strStr(self, haystack, needle): + """ + :type haystack: str + :type needle: str + :rtype: int + """ + # Empty needle edge-case + if needle == "": + return 0 + + index = 0 + while index + len(needle) <= len(haystack): + if haystack[index] == needle[0]: + if haystack[index:index+len(needle)] == needle: + return index + index += 1 + + return -1 \ No newline at end of file