Find the Index of the First Occurrence in a String

Solution
class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        n = len(haystack)
        m = len(needle)
        
        for i in range(n - m + 1):
            if haystack[i : i + m] == needle:
                return i
                
        return -1

Time Complexity

O(N M)

Space Complexity

O(1)