Skip to content

Commit

Permalink
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.9 MB (…
Browse files Browse the repository at this point in the history
…27.12%)
  • Loading branch information
jimit105 committed Feb 3, 2025
1 parent dca47fc commit e7607e1
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions 0028-find-the-index-of-the-first-occurrence-in-a-string/solution.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
# Approach - Sliding Window
# Approach 1: Sliding Window

# Time: O(mn)
# Space: O(1)

class Solution:
def strStr(self, haystack: str, needle: str) -> int:
if not needle:
return 0

n, k = len(haystack), len(needle)

if haystack[0 : k] == needle:
return 0

for i in range(k, n):
if haystack[i - k + 1 : i + 1] == needle:
return i - k + 1
m = len(needle)
n = len(haystack)

for window_start in range(n - m + 1):
for i in range(m):
if needle[i] != haystack[window_start + i]:
break
if i == m - 1:
return window_start

return -1

0 comments on commit e7607e1

Please sign in to comment.