""" 0028.1 - Find the Index of the First Occurrence in a String - Solution 1 - Brute Force """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def strStr(self, haystack: str, needle: str) -> int:
"""Find the index of the first occurrence of needle in haystack"""
m, n = len(haystack), len(needle)
for i in range(m - n + 1):
if haystack[i:i + n] == needle:
return i
return -1
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().strStr("sadbutsad", "sad")) # Expected: 0
print(Solution().strStr("leetcode", "leeto")) # Expected: -1
print(Solution().strStr("hello", "ll")) # Expected: 2
print(Solution().strStr("a", "a")) # Expected: 0
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0028.2 - Find the Index of the First Occurrence in a String - Solution 2 - Using find() """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def strStr(self, haystack: str, needle: str) -> int:
"""Find the index of the first occurrence of needle in haystack"""
return haystack.find(needle)
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().strStr("sadbutsad", "sad")) # Expected: 0
print(Solution().strStr("leetcode", "leeto")) # Expected: -1
print(Solution().strStr("hello", "ll")) # Expected: 2
print(Solution().strStr("a", "a")) # Expected: 0
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0028.3 - Find the Index of the First Occurrence in a String - Solution 3 - Char by Char """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def strStr(self, haystack: str, needle: str) -> int:
"""Find the index of the first occurrence of needle in haystack"""
m, n = len(haystack), len(needle)
for i in range(m - n + 1):
match = True
for j in range(n):
if haystack[i + j] != needle[j]:
match = False
break
if match:
return i
return -1
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().strStr("sadbutsad", "sad")) # Expected: 0
print(Solution().strStr("leetcode", "leeto")) # Expected: -1
print(Solution().strStr("hello", "ll")) # Expected: 2
print(Solution().strStr("a", "a")) # Expected: 0
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()