Python - Maximum Consecutive Substring Occurrence
Last Updated :
08 May, 2023
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:
- Initialize a variable "sub_str" with the substring to be searched.
- Calculate the length of the input string "test_str" and the length of the substring "sub_str".
- Initialize a variable "max_sub" with an empty string.
- 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.
- Multiply the substring by the current value of the loop counter and check if it exists in the input string "test_str".
- 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.
- 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)
OutputThe 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
- 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.
- 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)
OutputThe 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
- Initialize a variable, max_count, to 0 to keep track of the maximum count of the substring in any window.
- Initialize a variable, max_sub, to an empty string to keep track of the substring with the maximum count.
- Split the input string into a list of words using the split() method.
- Loop over the words in the list.
- For each word, count the number of occurrences of the given substring using the count() method.
- If the count is greater than or equal to max_count, update max_count and max_sub.
- Repeat steps 4-6 for all words in the list.
- 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)
OutputThe 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 discu
5 min read
Python - Maximum occurring Substring from list Sometimes, while working with Python strings, we can have a problem in which we need to check for maximum occurring substring from strings list. This can have application in DNA sequencing in Biology and other application. Let's discuss certain way in which this task can be performed. Method 1 : Usi
6 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 fin
8 min read
Python - First K consecutive digits in String Given a String and number K, extract first K consecutive digits making number. Input : test_str = "geeks5geeks43best", K = 2 Output : 43 Explanation : 43 is first 2 consecutive digits. Input : test_str = "geeks5gee2ks439best", K = 3 Output : 439 Explanation : 439 is first 3 consecutive digits. Metho
5 min read
Find Longest Consecutive Letter and Digit Substring - Python The task is to identify the longest sequence of consecutive letters or digits in a given string. A consecutive letter or digit refers to a substring where each character is adjacent to the next one in the alphabet for letters or numerically for digits. It involves identifying the longest sequence of
4 min read
Python - All occurrences of substring in string A substring is a contiguous occurrence of characters within a string. Identifying all instances of a substring is important for verifying various tasks. In this article, we will check all occurrences of a substring in String.Using re.finditer()re.finditer() returns an iterator yielding match objects
3 min read
Get Second Occurrence of Substring in Python String We are given a string and a substring, and our task is to find the index of the second occurrence of that substring within the string. This means we need to identify not just if the substring exists, but where it appears for the second time. For example, if we have a string like "hello world, hello
2 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_s
4 min read
Python | Get the starting index for all occurrences of given substring 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 # Python3 code to demonstrate # to find all occurrences of substring in # a
3 min read
Python - Minimum identical consecutive Subarray 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 that has the minimum consecutive occurrence. The knowledge of solution to it can be of great help and can be employed whenever required. Letâs discuss a
3 min read