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++
#include <bits/stdc++.h>
using namespace std;
string MaxFreq(string str)
{
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]++;
}
}
int maxi = 0;
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 s;
}
int main()
{
string str = "ababecdecd" ;
cout << MaxFreq(str);
return 0;
}
|
Java
import java.util.*;
class GFG{
static String MaxFreq(String str)
{
int n = str.length();
Map<String,
Integer> mp = new HashMap<String,
Integer>();
for ( int i = 0 ; i < n; i++)
{
String s = "" ;
for ( int j = i; j < n; j++)
{
s += str.charAt(j);
if (mp.containsKey(s))
{
mp.put(s, mp.get(s) + 1 );
}
else
{
mp.put(s, 1 );
}
}
}
int maxi = 0 ;
String s = "" ;
for (Map.Entry<String,
Integer> i : mp.entrySet())
{
if (i.getValue() > maxi)
{
maxi = i.getValue();
s = i.getKey();
}
else if (i.getValue() == maxi)
{
String ss = i.getKey();
if (ss.length() > s.length())
s = ss;
}
}
return s;
}
public static void main(String[] args)
{
String str = "ababecdecd" ;
System.out.print(MaxFreq(str));
}
}
|
Python3
def MaxFreq(s):
n = len (s)
m = dict ()
for i in range (n):
string = ''
for j in range (i, n):
string + = s[j]
if string in m.keys():
m[string] + = 1
else :
m[string] = 1
maxi = 0
maxi_str = ''
for i in m:
if m[i] > maxi:
maxi = m[i]
maxi_str = i
elif m[i] = = maxi:
ss = i
if len (ss) > len (maxi_str):
maxi_str = ss
return maxi_str
string = "ababecdecd"
print (MaxFreq(string))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static string MaxFreq( string str)
{
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;
}
}
}
int maxi = 0;
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 s;
}
public static void Main( string [] args)
{
string str = "ababecdecd" ;
Console.Write(MaxFreq(str));
}
}
|
Javascript
<script>
function MaxFreq(str)
{
let n = str.length;
let mp = new Map();
for (let i = 0; i < n; i++)
{
let s = "" ;
for (let j = i; j < n; j++)
{
s += str[j];
if (mp.has(s))
{
mp.set(s, mp.get(s) + 1);
}
else
{
mp.set(s, 1);
}
}
}
let maxi = 0;
let s = "" ;
for (let [key, value] of mp.entries())
{
if (value > maxi)
{
maxi = value;
s = key;
}
else if (value == maxi)
{
let ss = key;
if (ss.length > s.length)
s = ss;
}
}
return s;
}
let str = "ababecdecd" ;
document.write(MaxFreq(str));
</script>
|
Time Complexity: O(n2), where n is the length of the given string.
Auxiliary Space: O(n), where n is the length of the given string.
Last Updated :
08 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...