Maximum length substring with highest frequency in a string

Given a string. The task is to find the maximum occurred substring with a maximum length. These occurrences can overlap.

Examples:

Input: str = "abab"
Output: ab
"a", "b", "ab" are occur 2 times. But, "ab" has maximum length

Input: str = "abcd"
Output: a

Approach: The idea is to store the frequency of each substring using a map and print the one with maximum frequency and maximum length.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find maximum 
// occurred substring of a string 
#include <bits/stdc++.h> 
using namespace std; 
  
// function to return maximum 
// occurred substring of a string 
string MaxFreq(string str) 
    // size of the string 
    int n = str.size(); 
  
    unordered_map<string, int> m; 
  
    for (int i = 0; i < n; i++) { 
        string s = ""
        for (int j = i; j < n; j++) { 
            s += str[j]; 
            m[s]++; 
        
    
  
    // to store maximum frequency 
    int maxi = 0; 
  
    // to store string which has maximum frequency 
    string s; 
    for (auto i = m.begin(); i != m.end(); i++) { 
        if (i->second > maxi) { 
            maxi = i->second; 
            s = i->first; 
        
        else if (i->second == maxi) { 
            string ss = i->first; 
            if (ss.size() > s.size()) 
                s = ss; 
        
    
  
    // return substring which has maximum frequency 
    return s; 
  
// Driver program 
int main() 
    string str = "ababecdecd"
  
    // function call 
    cout << MaxFreq(str); 
  
    return 0; 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find maximum 
# occured of a string 
  
# function to return maximum occurred 
# substring of a string 
def MaxFreq(s): 
      
    # size of string 
    n = len(s) 
      
    m = dict() 
      
    for i in range(n): 
        strng = '' 
        for j in range(i, n): 
            strng += s[j] 
            if strng in m.keys(): 
                m[strng] += 1
            else
                m[strng] = 1
                  
    # to store maximum freqency 
    maxi = 0
      
    # To store string which has 
    # maximum frequency 
    maxi_str = '' 
      
    for i in m: 
        if m[i] > maxi: 
            maxi = m[i] 
            maxi_str =
        elif m[i] == maxi: 
            ss =
            if len(ss) > len(maxi_str): 
                maxi_str = ss 
                  
    # return substring which has maximum freq 
    return maxi_str 
      
# Driver code 
strng = "ababecdecd"
  
print(MaxFreq(strng)) 
          
# This code is contributed by Mohit kumar 29     

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find maximum 
// occurred substring of a string 
using System; 
using System.Collections.Generic;
  
class GFG{ 
  
// Function to return maximum
// occurred substring of a string
static string MaxFreq(string str)
{
      
    // Size of the string
    int n = str.Length;
      
    Dictionary<string
               int> m = new Dictionary<string,
                                       int>();
                          
    for(int i = 0; i < n; i++)
    {
        string sp = "";
        for(int j = i; j < n; j++)
        {
            sp += str[j];
            if(m.ContainsKey(sp))
            {
                m[sp]++;
            }
            else
            {
                m[sp] = 1; 
            }
        }
    }
  
    // To store maximum frequency
    int maxi = 0;
  
    // To store string which has maximum frequency
    string s = "";
      
    foreach(KeyValuePair<string, int> i in m) 
    
        if (i.Value > maxi)
        {
            maxi = i.Value;
            s = i.Key;
        }
        else if (i.Value == maxi)
        {
            string ss = i.Key;
            if (ss.Length > s.Length)
                s = ss;
        }
    
  
    // Return substring which has
    // maximum frequency
    return s;
}
  
// Driver Code 
public static void Main(string[] args) 
    string str = "ababecdecd";
  
    // Function call
    Console.Write(MaxFreq(str));
}
  
// This code is contributed by rutvik_56

chevron_right


Output:

ecd

competitive-programming-img




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohit kumar 29, rutvik_56