VOOZH about

URL: https://www.geeksforgeeks.org/python/python-maximum-occurring-substring-from-list/

⇱ Python - Maximum occurring Substring from list - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python - Maximum occurring Substring from list

Last Updated : 8 May, 2023

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 : Using regex() + groupby() + max() + lambda 

The combination of above functionalities can be used to solve this particular problem. In this, we first extract the sequences using regex function. Then the counter grouping is performed using groupby(). The last step is extracting maximum which is done using max() along with lambda function.


Output : 
The original string is : gfghsisbjknlmkesbestgfgsdcngfgcsdjnisdjnlbestdjsklgfgcdsbestbnjdsgfgdbhisbhsbestdkgfgb
The original list is : ['gfg', 'is', 'best']
Maximum frequency substring : gfg

 

Time complexity: O(n), where n is the length of the input string. The time complexity of regex(), groupby(), and max() is O(n).
Auxiliary space: O(k), where k is the length of the input list. This is the space needed to store the list of substrings. The space complexity of regex(), groupby(), and max() is O(1).

Method 2: Using count() and max() methods

count() returns the occurrence of a particular element in a sequence and the max() method returns the maximum of that.


Output
The original string is : gfghsisbjknlmkesbestgfgsdcngfgcsdjnisdjnlbestdjsklgfgcdsbestbnjdsgfgdbhisbhsbestdkgfgb
The original list is : ['gfg', 'is', 'best']
Maximum frequency substring : gfg

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

Method 3: Using re.findall() + Counter

This is an alternate approach that uses re.findall() and Counter module. In this, we extract the sequence using re.findall() and count the occurrence of each element using Counter() from collections module.


Output
The original string is : gfghsisbjknlmkesbestgfgsdcngfgcsdjnisdjnlbestdjsklgfgcdsbestbnjdsgfgdbhisbhsbestdkgfgb
The original list is : ['gfg', 'is', 'best']
Maximum frequency substring : gfg

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

Method 4 : Using operator.countOf() and max() methods


Output
The original string is : gfghsisbjknlmkesbestgfgsdcngfgcsdjnisdjnlbestdjsklgfgcdsbestbnjdsgfgdbhisbhsbestdkgfgb
The original list is : ['gfg', 'is', 'best']
Maximum frequency substring : gfg

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

Method 5: Using a dictionary to count occurrences

In this approach, we can use a dictionary to count the occurrences of each substring in the list. We can iterate over the string and for each substring in the list, we can count the number of occurrences of that substring in the string and update the count in the dictionary. Finally, we can find the substring with the maximum count in the dictionary.

Approach:

  1. Initialize an empty dictionary to count the occurrences of substrings.
  2. Iterate over the string using a for loop.
  3. For each substring in the list, find the number of occurrences of that substring in the string using the count() method and update the count in the dictionary.
  4. Find the substring with the maximum count in the dictionary.
  5. Return the maximum frequency substring.

Example:


Output
The original string is : gfghsisbjknlmkesbestgfgsdcngfgcsdjnisdjnlbestdjsklgfgcdsbestbnjdsgfgdbhisbhsbestdkgfgb
The original list is : ['gfg', 'is', 'best']
Maximum frequency substring : gfg

Time complexity: O(n*m), where n is the length of the string and m is the total number of substrings in the list.
Auxiliary space: O(m), where m is the total number of substrings in the list.

Method 6: Using itertools.product() and count()

  1. Import the product function from the itertools module.
  2. Use the product() function to generate all possible substrings of length len(sub) for each substring sub in test_list.
  3. Count the number of occurrences of each substring using the count() method.
  4. Initialize a variable max_count to 0 and a variable max_substring to an empty string.
  5. Loop through the substrings and their counts.
  6. If the current count is greater than max_count, update max_count and max_substring to the corresponding substring.
  7. Print the maximum occurring substring.

Example:


Output
The original string is : gfghsisbjknlmkesbestgfgsdcngfgcsdjnisdjnlbestdjsklgfgcdsbestbnjdsgfgdbhisbhsbestdkgfgb
The original list is : ['gfg', 'is', 'best']
Maximum frequency substring : gfg

Time complexity: O(n*m^2), where n is the length of test_list and m is the maximum length of a substring in test_list.
Auxiliary space: O(m)

Comment