class Solution: def strStr(self, haystack: str, needle: str) -> int: return haystack.find(needle) def main(): s = Solution() print(s.strStr("hello", "ll")) # 2 print(s.strStr("aaaaa", "bba")) # -1 if __name__ == '__main__': main()
class Solution: def strStr(self, haystack: str, needle: str) -> int: if len(haystack) == 0 and len(needle) == 0: return 0 if len(needle) == 0: return 0 i = 0 # pointer for haystack j = 0 # pointer for needle while i < len(haystack): if haystack[i] == needle[j]: print(i, j, haystack[i], needle[j]) i += 1 j += 1 else: print(i, j, haystack[i], needle[j]) if j == 0: i += 1 else: i -= j-1 j = 0 if j >= len(needle): break if j == len(needle): return i-len(needle) else: return -1 def main(): s = Solution() print(s.strStr("hello", "ll")) # 2 print(s.strStr("aaaaa", "bba")) # -1 print(s.strStr("abcdcdeef", "cde")) # 4 print(s.strStr("mississippi", "issip")) # 4 print(s.strStr("mississippi", "issipi")) # -1 print(s.strStr("a", "")) # 0 if __name__ == '__main__': main()