Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = "hello", needle = "ll"
Output: 2
Example 2:
Input: haystack = "aaaaa", needle = "bba"
Output: -1
Clarification:
What should we return when needle is an empty string? This is a great question to ask during an interview.
For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().
class Solution:
     
     def prefixTable(self, s):
         t = [0 for _ in s]
         j = 0
         i = 1
         while i < len(s):
             if j == 0 and s[i] != s[j]:
                 t[i] = 0
                 i += 1
             elif s[i] == s[j]:
                 t[i] = j + 1
                 i += 1
                 j += 1
             elif j > 0 and s[j] != s[i]:
                 j = t[j-1]
         return t
 
     def find(self, text, pattern):
         ans = []
         t = self.prefixTable(pattern)
         i = j = 0
         while i < len(text):
             if text[i] == pattern[j]:
                 i += 1
                 j += 1
 
             if j == len(pattern):
                 ans.append(i - len(pattern))
                 j = t[j-1]
 
             elif i < len(text) and text[i] != pattern[j]:
                 if j > 0:
                     j = t[j-1]
                 else:
                     i += 1
         return ans
     
     def strStr(self, haystack, needle):
         """
         :type haystack: str
         :type needle: str
         :rtype: int
         """
         if len(needle) == 0:
             return 0
         ans = self.find(haystack, needle)
         return ans[0] if ans else -1
         # for i in range(len(haystack) - len(needle) + 1):
         #     if haystack[i: i + len(needle)] == needle:
         #         return i
         # return -1
         
         
         # for i in range(len(haystack) - len(needle) + 1):
         #     if haystack[i : i + len(needle)] == needle:
         #         return i
         # return -1

class Solution:
     
     def prefixTable(self, s):
         t = [0 for _ in s]
         j = 0
         i = 1
         while i < len(s):
             if j == 0 and s[i] != s[j]:
                 t[i] = 0
                 i += 1
             elif s[i] == s[j]:
                 t[i] = j + 1
                 i += 1
                 j += 1
             elif j > 0 and s[j] != s[i]:
                 j = t[j-1]
         return t
 
     def find(self, text, pattern):
         ans = []
         t = self.prefixTable(pattern)
         i = j = 0
         while i < len(text):
             if text[i] == pattern[j]:
                 i += 1
                 j += 1
 
             if j == len(pattern):
                 ans.append(i - len(pattern))
                 j = t[j-1]
 
             elif i < len(text) and text[i] != pattern[j]:
                 if j > 0:
                     j = t[j-1]
                 else:
                     i += 1
         return ans
     
     def strStr(self, haystack, needle):
         """
         :type haystack: str
         :type needle: str
         :rtype: int
         """
         if len(needle) == 0:
             return 0
         ans = self.find(haystack, needle)
         return ans[0] if ans else -1
         # for i in range(len(haystack) - len(needle) + 1):
         #     if haystack[i: i + len(needle)] == needle:
         #         return i
         # return -1
         
         
         # for i in range(len(haystack) - len(needle) + 1):
         #     if haystack[i : i + len(needle)] == needle:
         #         return i
         # return -1
