I am trying to write the sublist search algorithm using Python.
For reference : https://www.geeksforgeeks.org/sublist-search-search-a-linked-list-in-another-list/
Here is my code:
def sublist(arr1,arr2):
i = 0
j = 0
k = 0
if len(arr1) == 0:
print('List1 Empty')
if len(arr2) == 0:
print('List 2 Empty')
for j in range(0,len(arr2)):
for i in range(0,len(arr1)):
if arr1[i] != arr2[j]:
break
while arr1[i] == arr2 [j]:
if i == len(arr1) - 1:
return True
i = i + 1
j = j + 1
if i == len(arr1):
return False
return False
I am sure this code can be optimized and reduce time complexity. Because of while loop, does the complexity increase than O(m*n)? Beginner student here