Consider Following Input
sub_str = "hij"
input_strs = "abcdefghij"
Logic here is -
Check string is sub string or not by group of sequential order of main string
start from 0 to end of string.
Iterations are like following -
Iteration 1: abc
Iteration 2: bcd
Iteration 3: cde
Iteration 4: def
Iteration 5: efg
Iteration 6: fgh
Iteration 7: ghi
Iteration 8: hij
Maximum 8 iteration is required when Length of Main string is 8 and Sub string is 3.
Complexity:
Worst case complexity = LenOfMainString - LenOfSubString + 1
Best case complexity = 0 when LenOfSubString is greater than LenOfMainString
Note: This is code to find is given string in present in main string or not i.e. sub-string. Not to get index, but code print index if match else print -1
Code
def is_sub_string(main_str, sub_str):
"""
@Summary: Check string is sub string of main or not
@Param main_str(String): Main string in which we have to check sub string is
present or not.
@Param sub_str(String): String which we want to check if present in main
string or not.
@Return (Boolean): True if present else False.
"""
# Length of main string and sub string
# We will iterate over main string is input_str_len - sub_len + 1
# Means if main string have 10 characters and sub string have 3 characters
# then in worst case if have to iterate 8 time because last two character
# can not be sub string, as sub string length is 3
sub_len = len(sub_str)
input_str_len = len(main_str)
index = 0
is_sub_string = False
while index<input_str_len-sub_len+1:
# Check sub_str is equal to sequential group of same characters in main
# string.
if sub_str==main_str[index:index+sub_len]:
is_sub_string = True
break
# Increase index count by one to move to next character.
index += 1
print("Total Iteration:", index + 1 if is_sub_string else index, end="\t")
print("Is Substring:", is_sub_string, end="\t")
print("Index:", index if is_sub_string else -1)
return is_sub_string
Output
Case 01: When string present at start of main string.
status = is_sub_string("abcdefghij", "abc")
>> Total Iteration: 1 Is Substring: True Index: 0
Case 02: When string present at end of main string.
status = is_sub_string("abcdefghij", "hij")
>> Total Iteration: 8 Is Substring: True Index: 7
Case 03: When string not present in main string.
status = is_sub_string("abcdefghij", "hix")
>>Total Iteration: 8 Is Substring: False Index: -1
Case 04: When string length is more than main string.
status = is_sub_string("abcdefghij", "abcdefghijabcdefghij")
>>Total Iteration: 0 Is Substring: False Index: -1
OR
You can reduce iteration count by half if we search string in starting as well as from ending.
Complexity
Worst case complexity = (LenOfMainString - LenOfSubString + 1)/2
Best case complexity = 0 when LenOfSubString is greater than LenOfMainString
Code
def is_sub_string(main_str, sub_str):
"""
@Summary: Check string is sub string of main or not
@Param main_str(String): Main string in which we have to check sub string is
present or not.
@Param sub_str(String): String which we want to check if present in main
string or not.
@Return (Boolean): True if present else False.
"""
# Length of main string and sub string
# We will iterate over main string is (main_str_len - sub_len + 1)/2
sub_len = len(sub_str)
input_str_len = len(main_str)
index = 0
is_sub_string = False
find_index = -1
while index<(input_str_len-sub_len+1)/2:
# Check sub_str is equal to sequential group of same characters in main
# string.
if sub_str==main_str[index:index+sub_len]:
is_sub_string = True
find_index = index
break
print((index+sub_len)*-1, input_str_len-index, end="\t")
print(main_str[(index+sub_len)*-1:input_str_len-index], main_str[index:index+sub_len])
if sub_str==main_str[(index+sub_len)*-1:input_str_len-index]:
is_sub_string = True
find_index = (index+sub_len-input_str_len) * (-1)
break
# Increase index count by one to move to next characters.
index += 1
print("Total Iteration:", index + 1 if is_sub_string else index, end="\t")
print("Is Substring:", is_sub_string, end="\t")
print("Index:", find_index)
return is_sub_string