Given a string and a substring, write a Python program to find how many numbers of substrings are there in the string (including overlapping cases). Let’s discuss a few methods below.
Method #1: Using re.findall() Method
Python3
import re
s = 'ababababa'
res = len (re.findall( '(?=(aba))' , s))
print ( "Number of substrings" , res)
|
Output
Number of substrings 4
Method #2: Using startswith()
Python3
ini_str = "ababababa"
sub_str = 'aba'
res = sum ( 1 for i in range ( len (ini_str))
if ini_str.startswith( "aba" , i))
print ( "Number of substrings" , res)
|
Output:
Number of substrings 4
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #3: Using count() Method
Python3
ini_str = "ababababa"
sub_str = 'aba'
def Countofoccurrences(ini_str,sub_str):
count = 0
start = 0
while start < len (ini_str):
pos = ini_str.find(sub_str, start)
if pos ! = - 1 :
start = pos + 1
count + = 1
else :
break
return count
print ( "Number of substrings" , Countofoccurrences(ini_str,sub_str))
|
Output
Number of substrings 4
Method #4: Using List Comprehension
In this method, we are using a list comprehension to check if the substring is present in the main string or not. We are using the range of length of the main string minus the length of the substring plus 1. We are then checking if the substring present in the main string at the current index is equal to the given substring or not and incrementing the count accordingly.
Python3
ini_str = "ababababa"
sub_str = 'aba'
res = sum ([ 1 for i in range ( len (ini_str) - len (sub_str) + 1 ) if ini_str[i:i + len (sub_str)] = = sub_str])
print ( "Number of substrings" , res)
|
Output
Number of substrings 4
Time Complexity: O(n)
Auxiliary Space: O(1)
Approach#4: Using sliding window
This program uses a sliding window technique to slide a window of size m over the input string ini_str. At each step, it checks if the substring in the window is equal to sub_str, and if it is, it increments the count variable. It slides the window by iterating over the indices of the input string from 0 to n – m + 1.
Algorithm
1. Initialize count variable to 0
2. Find length of the input string ini_str and length of the substring to be counted sub_str
3. Iterate over the indices of the input string from 0 to n – m + 1
4. At each index, extract a substring of length m from ini_str
5. Compare the extracted substring with the sub_str, if it matches, increment the count variable
6. Return the count variable
Python3
ini_str = "ababababa"
sub_str = 'aba'
count = 0
n = len (ini_str)
m = len (sub_str)
for i in range (n - m + 1 ):
if ini_str[i:i + m] = = sub_str:
count + = 1
print ( "Number of substrings:" , count)
|
Output
Number of substrings: 4
Time complexity: O(n * m), where n is the length of the input string and m is the length of the substring to be counted. This is because the program iterates over the input string ini_str n – m + 1 times, and at each step, it extracts a substring of length m and compares it with sub_str.
Space complexity: O(1), as it only uses a constant amount of additional space to store the count variable, the length of the input string n, and the length of the substring to be counted m. It does not create any new data structures or use any additional memory proportional to the size of the input.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
19 Apr, 2023
Like Article
Save Article