Python | Get the starting index for all occurrences of given substring
Last Updated :
24 Mar, 2023
Given a string and a substring, the task is to find out the starting index for all the occurrences of a given substring in a string. Let’s discuss a few methods to solve the given task.
Method #1: Using Naive Method
Python3
ini_string = 'xbzefdgstbzefzexezef'
sub_string = 'zef'
print ( "initial_strings : " , ini_string, "\nsubstring : " , sub_string)
res = []
flag = 0
k = 0
for i in range ( 0 , len (ini_string)):
k = i
flag = 0
for j in range ( 0 , len (sub_string)):
if ini_string[k] ! = sub_string[j]:
flag = 1
if flag:
break
k = k + 1
if flag = = 0 :
res.append(i)
print ( "resultant positions" , str (res))
|
Output:
initial_strings : xbzefdgstbzefzexezef
substring : zef
resultant positions [2, 10, 17]
Time Complexity: O(n2)
Auxiliary Space O(n)
Method #2: Using list comprehension
Python3
ini_string = 'xbzefdgstbzefzexezef'
sub_string = 'zef'
print ( "initial_strings : " , ini_string, "\nsubstring : " , sub_string)
res = []
res = [i for i in range ( len (ini_string))
if ini_string.startswith(sub_string, i)]
print ( "resultant positions" , str (res))
|
Output:
initial_strings : xbzefdgstbzefzexezef
substring : zef
resultant positions [2, 10, 17]
Time Complexity: O(n2)
Auxiliary Space O(n)
Method #3: Using regex
Python3
import re
ini_string = 'xbzefdgstbzefzexezef'
sub_string = 'zef'
print ( "initial_strings : " , ini_string, "\nsubstring : " , sub_string)
res = []
res = [m.start() for m in re.finditer(sub_string, ini_string)]
print ( "resultant positions" , str (res))
|
Output:
initial_strings : xbzefdgstbzefzexezef
substring : zef
resultant positions [2, 10, 17]
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method #4 : Using find() and replace() methods
Python3
ini_string = 'xbzefdgstbzefzexezef'
sub_string = 'zef'
print ( "initial_strings : " , ini_string, "\nsubstring : " , sub_string)
res = []
while (ini_string.find(sub_string) ! = - 1 ):
res.append(ini_string.find(sub_string))
ini_string = ini_string.replace(sub_string, "*" * len (sub_string), 1 )
print ( "resultant positions" , str (res))
|
Output
initial_strings : xbzefdgstbzefzexezef
substring : zef
resultant positions [2, 10, 17]
Time Complexity: O(n2)
Auxiliary Space: O(n)
Using str.index() in a loop:
Approach:
In this example, the string variable contains the string we want to search in, and the substring variable contains the substring we want to find. We pass these variables as arguments to the get_substring_indices() function and store the result in the indices variable. Finally, we print the indices variable to see the starting index for all occurrences of the given substring.
Python3
def get_substring_indices(string, substring):
indices = []
try :
index = string.index(substring)
while index ! = - 1 :
indices.append(index)
index = string.index(substring, index + 1 )
except ValueError:
pass
return indices
string = "hello world, world is beautiful"
substring = "world"
indices = get_substring_indices(string, substring)
print (indices)
|
Time Complexity: O(n*m), where n is the length of the string and m is the length of the substring
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...