Open In App

Python – Maximum Consecutive Substring Occurrence

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python, we can have a problem in which we must check for substrings occurring in consecutive repetition. This can have applications in data domains. Let us discuss a way in which this task can be performed. 

Method #1: Using max() + re.findall() 

A combination of the above methods can be used to perform this task. In this, we extract the substrings repetitions using findall() and the maximum of them using max(). 

Python3




# Python3 code to demonstrate working of
# Maximum Consecutive Substring Occurrence
# Using max() + re.findall()
import re
 
# Initializing string
test_str = 'geeksgeeks are geeks for all geeksgeeksgeeks'
 
# Printing original string
print("The original string is : " + str(test_str))
 
# Initializing subs
sub_str = 'geeks'
 
# Maximum Consecutive Substring Occurrence
# using max() + re.findall()
res = max(re.findall('((?:' + re.escape(sub_str) + ')*)', test_str), key=len)
 
# Printing result
print("The maximum run of Substring : " + res)


Output : 

The original string is : geeksgeeks are geeks for all geeksgeeksgeeks
The maximum run of Substring : geeksgeeksgeeks

The Time and Space Complexity for all the methods is the same:

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2: Using list comprehension+ loop + max() + len()

Algorithm:

  1. Initialize a variable “sub_str” with the substring to be searched.
  2. Calculate the length of the input string “test_str” and the length of the substring “sub_str”.
  3. Initialize a variable “max_sub” with an empty string.
  4. Loop through a range of values from 0 to the quotient of the length of the input string divided by the length of the substring.
  5. Multiply the substring by the current value of the loop counter and check if it exists in the input string “test_str”.
  6. If the substring exists in the input string, update the “max_sub” variable with the current substring if its length is greater than the length of the previous “max_sub” substring.
  7. Return the “max_sub” variable.

Python3




# initializing string
test_str = 'geeksgeeks are geeks for all geeksgeeksgeeks'
# initializing subs
sub_str = 'geeks'
# printing original string
print("The original string is : " + str(test_str))
 
max_sub = max([sub_str * n for n in range(len(test_str)//len(sub_str)+1) if sub_str * n in test_str], key=len)
 
# printing result
print("The maximum run of Substring : " + max_sub)


Output

The original string is : geeksgeeks are geeks for all geeksgeeksgeeks
The maximum run of Substring : geeksgeeksgeeks

Time complexity: O(n^2), where n is the length of the input string. This is because the nested loop runs for a maximum of n/len(sub_str) times and the inbuilt max() function also takes O(n) time.

Auxiliary Space: O(n), where n is the length of the input string. The space is used to store the input string, substring, and the “max_sub” variable.

Method #3: Using the lambda function

  1. In this method, we define a lambda function called max_substring that takes two arguments, a string s and a substring sub, and returns the maximum consecutive occurrence of the substring in the string.
  2. We then use the lambda function max_substring to find the maximum consecutive occurrence of the substring in the input string test_str. Finally, we print the result res.

Below is the code for the above method: 

Python3




# Python3 code to demonstrate working of
# Maximum Consecutive Substring Occurrence
import re
 
# initializing string
test_str = 'geeksgeeks are geeks for all geeksgeeksgeeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing subs
sub_str = 'geeks'
 
# using lambda function to find maximum consecutive substring
max_substring = lambda s, sub: max(re.findall('((?:' + re.escape(sub) + ')*)', s), key=len)
 
# Maximum Consecutive Substring Occurrence
res = max_substring(test_str, sub_str)
 
# printing result
print("The maximum run of Substring : " + res)


Output

The original string is : geeksgeeks are geeks for all geeksgeeksgeeks
The maximum run of Substring : geeksgeeksgeeks

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

Auxiliary Space: O(n^2)

Method 4:  Using Loops

  1. Initialize a variable, max_count, to 0 to keep track of the maximum count of the substring in any window.
  2. Initialize a variable, max_sub, to an empty string to keep track of the substring with the maximum count.
  3. Split the input string into a list of words using the split() method.
  4. Loop over the words in the list.
  5. For each word, count the number of occurrences of the given substring using the count() method.
  6. If the count is greater than or equal to max_count, update max_count and max_sub.
  7. Repeat steps 4-6 for all words in the list.
  8. Return the substring with the maximum count

Python3




# initializing string
test_str = 'geeksgeeks are geeks for all geeksgeeksgeeks'
# initializing subs
sub_str = 'geeks'
# printing original string
print("The original string is : " + str(test_str))
 
# initializing variables
max_count = 0
max_sub = ""
 
# split the input string into a list of words
words = test_str.split()
 
# loop over the words in the list
for word in words:
    # count the number of occurrences of the given substring in the word
    count = word.count(sub_str)
    # update max_count and max_sub if necessary
    if count >= max_count:
        max_count = count
        max_sub = sub_str * max_count
 
# printing result
print("The maximum run of Substring : " + max_sub)


Output

The original string is : geeksgeeks are geeks for all geeksgeeksgeeks
The maximum run of Substring : geeksgeeksgeeks

Time complexity: O(NK), where n is the number of words in the input string and k is the length of the substring. The count() method has a time complexity of O(k), and it is called once for each word in the input string.
Auxiliary space: O(K), where k is the length of the substring. The max_sub variable can have at most k characters.



Similar Reads

Python | Consecutive Maximum Occurrence in list
Sometimes, while working with Python lists or in competitive programming setup, we can come across a subproblem in which we need to get an element which has the maximum consecutive occurrence. The knowledge of the solution of it can be of great help and can be employed whenever required. Let's discuss certain ways in which this task can be performe
5 min read
Python | Find last occurrence of substring
Sometimes, while working with strings, we need to find if a substring exists in the string. This problem is quite common and its solution has been discussed many times before. The variation of getting the last occurrence of the string is discussed here. Let's discuss certain ways in which we can find the last occurrence of substring in string in Py
8 min read
Python | Get the string after occurrence of given substring
Sometimes, more than finding a substring, we might need to get the string that is occurring after the substring has been found. Let's discuss certain ways in which this task can be performed using Python. Using partition() to get string after occurrence of given substring The partition function can be used to perform this task in which we just retu
3 min read
Python | Ways to find nth occurrence of substring in a string
Given a string and a substring, write a Python program to find the nth occurrence of the string. Let's discuss a few methods to solve the given task. Get Nth occurrence of a substring in a String using regex Here, we find the index of the 'ab' character in the 4th position using the regex re.finditer() C/C++ Code import re # Initialising values ini
4 min read
Python program to find the occurrence of substring in the string
Given a list of words, extract all the indices where those words occur in the string. Input : test_str = 'geeksforgeeks is best for geeks and cs', test_list = ["best", "geeks"] Output : [2, 4] Explanation : best and geeks occur at 2nd and 4th index respectively. Input : test_str = 'geeksforgeeks is best for geeks and cs', test_list = ["best", "geek
4 min read
Get Second Occurrence of Substring in Python String
When we try to work in Python with the concepts of strings, this may create situations where you need to find out the second occurrence of a specific substring with a larger string. This may be achieved through out the different approaches as we know but we will try to explore Two different methods to do this. Find The Second Occurrence Of A Substr
3 min read
Python - Consecutive Alphabetic Occurrence
Sometimes, while working with Strings, we can have a problem in which we need to check whether we can find occurrence of characters consecutive and according to English alphabets. This kind of problem can occur in school programming and day-day programming. Lets discuss certain ways in which this task can be performed. Method #1 : Using loop + asci
4 min read
Python | Find longest consecutive letter and digit substring
Given a String (may contain both letters and digits), write a Python program to find the longest consecutive letter and digit substring. Examples: Input : geeks123available Output : ('available', 123) Input : 98apple658pine Output : ('apple', 658) Approach #1 : Brute force This is the Naive or brute force approach to find the longest consecutive le
5 min read
Python Program to get number of consecutive repeated substring
Given a substring K, the task is to write a Python Program to find the repetition of K string in each consecutive occurrence of K. Example Input : test_str = 'geeksgeeks are geeksgeeksgeeks for all geeks', K = "geeks" Output : [2, 3, 1] Explanation : First consecution of 'geeks' is 2. Input : test_str = 'geeksgeeks are geeksgeeksgeeksgeeks for all
4 min read
Length of the longest substring with consecutive characters
Given string str of lowercase alphabets, the task is to find the length of the longest substring of characters in alphabetical order i.e. string "dfabck" will return 3. Note that the alphabetical order here is considered circular i.e. a, b, c, d, e, ..., x, y, z, a, b, c, .... Examples: Input: str = "abcabcdefabc" Output: 6 All valid sub-strings ar
7 min read
Practice Tags :
three90RightbarBannerImg