Open In App

Python | Frequency of substring in given string

Last Updated : 02 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Finding a substring in a string has been dealt with in many ways. But sometimes, we are just interested to know how many times a particular substring occurs in a string. Let’s discuss certain ways in which this task is performed.

Method #1: Using count() This is a quite straightforward method in which this task is performed. It simply counts the occurrence of substrings in a string that we pass as an argument. 

Python3




# Python3 code to demonstrate working of
# Frequency of substring in string
# Using count()
 
# initializing string
test_str = "GeeksforGeeks is for Geeks"
 
# initializing substring
test_sub = "Geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# printing substring
print("The original substring : " + test_sub)
 
# using count()
# Frequency of substring in string
res = test_str.count(test_sub)
 
# printing result
print("The frequency of substring in string is " + str(res))


Output : 

The original string is : GeeksforGeeks is for Geeks
The original substring : Geeks
The frequency of substring in string is 3

Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(1), as we are not using any extra space in the form of data structures.

Method #2 : Using len() + split() The combination of above functions can be used to perform this task. This is performed in 2 steps, in 1st step, we split the string to list by the substring and then count the elements, which is 1 more than the required value. 

Python3




# Python3 code to demonstrate working of
# Frequency of substring in string
# Using split() + len()
 
# initializing string
test_str = "GeeksforGeeks is for Geeks"
 
# initializing substring
test_sub = "Geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# printing substring
print("The original substring : " + test_sub)
 
# using split() + len()
# Frequency of substring in string
res = len(test_str.split(test_sub))-1
 
# printing result
print("The frequency of substring in string is " + str(res))


Output : 

The original string is : GeeksforGeeks is for Geeks
The original substring : Geeks
The frequency of substring in string is 3

Time complexity: O(n), where n is the length of the string test_str.

Auxiliary space: O(1), since the only additional space used is for the variable res.

Method #3: Using re.findall()
The python re module provides a function findall() which can be used to find all occurrences of a substring in a string. This function returns a list of all non-overlapping occurrences of the substring in the string. The length of this list represents the frequency of the substring in the string.

Python3




import re
 
# initializing string
test_str = "GeeksforGeeks is for Geeks"
 
# initializing substring
test_sub = "Geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# printing substring
print("The original substring : " + test_sub)
 
# Using re.findall()
# Frequency of substring in string
res = len(re.findall(test_sub, test_str))
 
# printing result
print("The frequency of substring in string is " + str(res))


Output

The original string is : GeeksforGeeks is for Geeks
The original substring : Geeks
The frequency of substring in string is 3

Time complexity: O(n) where n is the length of the string. 
Auxiliary Space: O(m) where m is the number of occurrences of the substring in the string. 

Method #4: Using a loop

Algorithm:

Initialize a counter variable to 0.
Loop through each character in the string.
If the current character matches the first character of the substring, check if the following characters match the substring.
If the characters match the substring, increment the counter variable by 1.
Repeat the above steps until all characters in the string have been checked.
Return the counter variable as the frequency of the substring in the string.

Python3




# initializing string
test_str = "GeeksforGeeks is for Geeks"
 
# initializing substring
test_sub = "Geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# printing substring
print("The original substring : " + test_sub)
 
# Using a loop
# Frequency of substring in string
count = 0
for i in range(len(test_str)):
    if test_str[i:i+len(test_sub)] == test_sub:
        count += 1
 
# printing result
print("The frequency of substring in string is " + str(count))


Output

The original string is : GeeksforGeeks is for Geeks
The original substring : Geeks
The frequency of substring in string is 3

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), as we only need a constant amount of extra space for the counter variable.

Method #5 : Using the str.find() method

  1. Initialize the string test_str and the substring test_sub as mentioned in the code.
  2. Print the original string and substring.
  3. Initialize a variable count to 0 to keep track of the frequency.
  4. Initialize a variable index to 0.
  5. Use the str.find() method to find the first occurrence of the substring in the string, starting from the index position.
  6. Inside a while loop, check if the find() method returns a valid index (not -1).
  7. If a valid index is found, increment the count variable by 1.
  8. Update the index to the position of the previous match plus 1.
  9. Repeat steps 5-8 until the find() method returns -1.
  10. Print the result, which is the frequency of the substring in the string.

Python3




# initializing string
test_str = "GeeksforGeeks is for Geeks"
 
# initializing substring
test_sub = "Geeks"
 
# printing original string
print("The original string is : " + test_str)
 
# printing substring
print("The original substring : " + test_sub)
 
# Using str.find()
# Frequency of substring in string
count = 0
index = 0
while index != -1:
    index = test_str.find(test_sub, index)
    if index != -1:
        count += 1
        index += 1
 
# printing result
print("The frequency of substring in string is " + str(count))


Output

The original string is : GeeksforGeeks is for Geeks
The original substring : Geeks
The frequency of substring in string is 3

time complexity: O(n), where n is the length of the string.

 auxiliary space: O(1), as no additional data structures are used.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads