考虑跟随输入
sub_str = "hij"
input_strs = "abcdefghij"
这里的逻辑是 -
按组主串的先后顺序检查字符串是否为子串
从 0 开始到字符串结尾。
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
当主字符串长度为8,子字符串长度为3时,最多需要8次迭代。
复杂性:
Worst case complexity = LenOfMainString - LenOfSubString + 1
Best case complexity = 0 when LenOfSubString is greater than LenOfMainString
注意:这是要查找的代码是否存在于主字符串中,即子字符串中。不获取索引,但如果匹配则代码打印索引,否则打印-1
代码
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
输出
案例 01:当字符串出现在主字符串的开头时。
status = is_sub_string("abcdefghij", "abc")
>> Total Iteration: 1 Is Substring: True Index: 0
情况 02:当字符串出现在主字符串的末尾时。
status = is_sub_string("abcdefghij", "hij")
>> Total Iteration: 8 Is Substring: True Index: 7
案例 03:当主字符串中不存在字符串时。
status = is_sub_string("abcdefghij", "hix")
>>Total Iteration: 8 Is Substring: False Index: -1
案例04:当字符串长度大于主字符串时。
status = is_sub_string("abcdefghij", "abcdefghijabcdefghij")
>>Total Iteration: 0 Is Substring: False Index: -1
或
如果我们在开头和结尾搜索字符串,您可以将迭代次数减少一半。
复杂性
Worst case complexity = (LenOfMainString - LenOfSubString + 1)/2
Best case complexity = 0 when LenOfSubString is greater than LenOfMainString
代码
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