Substring of length K having maximum frequency in the given string
Last Updated :
14 Sep, 2023
Given a string str, the task is to find the substring of length K which occurs the maximum number of times. If more than one string occurs maximum number of times, then print the lexicographically smallest substring.
Examples:
Input: str = “bbbbbaaaaabbabababa”, K = 5
Output: ababa
Explanation:
The substrings of length 5 from the above strings are {bbbbb, bbbba, bbbaa, bbaaa, baaaa, aaaaa, aaaab, aaabb, aabba, abbab, bbaba, babab, ababa, babab, ababa}.
Among all of them, substrings {ababa, babab} occurs the maximum number of times(= 2).
The lexicographically smallest string from {ababa, babab} is ababa.
Therefore, “ababa” is the required answer.
Input: str = “heisagoodboy”, K = 5
Output: agood
Explanation:
The substrings of length 5 from the above string are {heisa, eisag, isago, sagoo, agood, goodb, oodbo, odboy}.
All of them occur only once. But the lexicographically smallest string among them is “agood”.
Therefore, “agood” is the required answer.
Naive Approach: The simplest approach to solve the problem is to generate all the substrings of size K from the given string and store the frequency of each substring in a Map. Then, traverse the Map and find the lexicographically smallest substring which occurs maximum number of times and print it.
C++
#include <bits/stdc++.h>
using namespace std;
string maximumOccurringString(string str, int k)
{
string curr= "" ;
int i=0, j=0, n=str.length();
map<string, int >mp;
while (j<n){
curr+=str[j];
if (j-i+1 < k){
j++;
}
else if (j-i+1 == k){
mp[curr]++;
curr.erase(0, 1);
i++;
j++;
}
}
int cnt=-1;
string ans;
for ( auto x : mp){
int c = x.second;
if (c > cnt){
ans = x.first;
cnt =c;
}
}
return ans;
}
int main()
{
string str = "bbbbbaaaaabbabababa" ;
int k = 5;
cout << maximumOccurringString(str, k);
return 0;
}
|
Java
import java.util.*;
public class Main {
static String maximumOccurringString(String str, int k) {
String curr = "" ;
int i = 0 , j = 0 , n = str.length();
TreeMap<String, Integer> mp = new TreeMap<>();
while (j < n) {
curr += str.charAt(j);
if (j - i + 1 < k) {
j++;
}
else if (j - i + 1 == k) {
mp.put(curr, mp.getOrDefault(curr, 0 ) + 1 );
curr = curr.substring( 1 );
i++;
j++;
}
}
int cnt = - 1 ;
String ans = "" ;
for (Map.Entry<String, Integer> x : mp.entrySet()) {
int c = x.getValue();
if (c > cnt) {
ans = x.getKey();
cnt = c;
}
}
return ans;
}
public static void main(String[] args) {
String str = "bbbbbaaaaabbabababa" ;
int k = 5 ;
System.out.println(maximumOccurringString(str, k));
}
}
|
Python3
def maximum_occuring_string(string, k):
curr = ""
n = len (string)
i = j = 0
mp = {}
while j < n:
curr + = string[j]
if j - i + 1 < k:
j + = 1
elif j - i + 1 = = k:
if curr in mp:
mp[curr] + = 1
else :
mp[curr] = 1
curr = curr[ 1 :]
i + = 1
j + = 1
cnt = - 1
ans = ""
for x in mp:
c = mp[x]
if c > cnt or (c = = cnt and x < ans):
ans = x
cnt = c
return ans
string = "bbbbbaaaaabbabababa"
k = 5
print (maximum_occuring_string(string, k))
|
C#
using System;
using System.Collections.Generic;
class Program
{
static string MaximumOccurringString( string str, int k)
{
string curr = "" ;
int i = 0, j = 0, n = str.Length;
SortedDictionary< string , int > mp = new SortedDictionary< string , int >();
while (j < n)
{
curr += str[j];
if (j - i + 1 < k)
{
j++;
}
else if (j - i + 1 == k)
{
if (mp.ContainsKey(curr))
{
mp[curr]++;
}
else
{
mp.Add(curr, 1);
}
curr = curr.Substring(1);
i++;
j++;
}
}
int cnt = -1;
string ans = "" ;
foreach ( var x in mp)
{
int c = x.Value;
if (c > cnt)
{
ans = x.Key;
cnt = c;
}
}
return ans;
}
static void Main()
{
string str = "bbbbbaaaaabbabababa" ;
int k = 5;
Console.WriteLine(MaximumOccurringString(str, k));
}
}
|
Javascript
function MaximumOccurringString(str, k) {
let curr = "" ;
let i = 0, j = 0, n = str.length;
let mp = new Map();
while (j < n) {
curr += str[j];
if (j - i + 1 < k) {
j++;
}
else if (j - i + 1 == k) {
if (mp.has(curr)) {
mp.set(curr, mp.get(curr) + 1);
}
else {
mp.set(curr, 1);
}
curr = curr.substring(1);
i++;
j++;
}
}
let cnt = -1;
let ans = "" ;
let keys = Array.from(mp.keys())
keys.sort()
for (let key of keys) {
let c = mp.get(key);
if (c > cnt) {
ans = key;
cnt = c;
}
}
return ans;
}
let str = "bbbbbaaaaabbabababa" ;
let k = 5;
console.log(MaximumOccurringString(str, k));
|
Time Complexity: O(N*( K + log K))
Auxiliary Space: O(N * K)
Efficient Approach: To optimize the above approach, the idea is to use Sliding Window technique. Consider a window of size
K to generate all substrings of length K and count the frequency of a substring generated in a Map. Traverse the map and find the substring that occurs maximum number of times and print it. If several of them exist, then print the lexicographically smallest substring.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using ll = long long int ;
using namespace std;
void maximumOccurringString(string s, ll K)
{
map<deque< char >, ll> M;
ll i;
deque< char > D;
for (i = 0; i < K; i++) {
D.push_back(s[i]);
}
M[D]++;
D.pop_front();
for (ll j = i; j < s.size(); j++) {
D.push_back(s[j]);
M[D]++;
D.pop_front();
}
ll maxi = INT_MIN;
deque< char > ans;
for ( auto it : M) {
if (it.second > maxi) {
maxi = it.second;
ans = it.first;
}
}
for (ll i = 0; i < ans.size(); i++) {
cout << ans[i];
}
}
int main()
{
string s = "bbbbbaaaaabbabababa" ;
ll K = 5;
maximumOccurringString(s, K);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void maximumOccurringString(String s,
int K)
{
Map<String, Integer> M = new HashMap<>();
Deque<Character> D = new LinkedList<>();
for ( int i = 0 ; i < K; i++) {
D.addLast(s.charAt(i));
}
M.put(D.toString(),
M.getOrDefault(D.toString(), 0 ) + 1 );
D.removeFirst();
for ( int j = K; j < s.length(); j++) {
D.addLast(s.charAt(j));
M.put(D.toString(),
M.getOrDefault(D.toString(), 0 ) + 1 );
D.removeFirst();
}
int maxi = Integer.MIN_VALUE;
String ans = "" ;
for (String it : M.keySet()) {
if (M.get(it) > maxi) {
maxi = M.get(it);
ans = it;
}
}
for ( int i = 0 ; i < ans.length(); i++) {
char c = ans.charAt(i);
if (Character.isAlphabetic(c)) {
System.out.print(c);
}
}
}
public static void main(String[] args)
{
String s = "bbbbbaaaaabbabababa" ;
int K = 5 ;
maximumOccurringString(s, K);
}
}
|
Python3
from collections import deque, Counter, defaultdict
import sys
def maximumOccurringString(s, K):
M = {}
D = deque()
for i in range (K):
D.append(s[i])
M[ str ("".join( list (D)))] = M.get(
str ("".join( list (D))), 0 ) + 1
D.popleft()
for j in range (i, len (s)):
D.append(s[j])
M[ str ("".join( list (D)))] = M.get(
str ("".join( list (D))), 0 ) + 1
D.popleft()
maxi = - sys.maxsize - 1
ans = deque()
for it in M:
if (M[it] > = maxi):
maxi = M[it]
ans = it
for i in range ( len (ans)):
print (ans[i], end = "")
if __name__ = = '__main__' :
s = "bbbbbaaaaabbabababa"
K = 5
maximumOccurringString(s, K)
|
C#
using System;
using System.Collections.Generic;
namespace MaximumOccurringSubstring
{
class Program
{
static void maximumOccurringString( string s, long K)
{
Dictionary<Queue< char >, long > M = new Dictionary<Queue< char >, long >();
long i;
Queue< char > D = new Queue< char >();
for (i = 0; i < K; i++)
{
D.Enqueue(s[( int )i]);
}
M[D] = M.ContainsKey(D) ? M[D] + 1 : 1;
D.Dequeue();
for ( long j = i; j < s.Length; j++)
{
D.Enqueue(s[( int )j]);
M[D] = M.ContainsKey(D) ? M[D] + 1 : 1;
D.Dequeue();
}
long maxi = int .MinValue;
Queue< char > ans = new Queue< char >();
foreach ( var kvp in M)
{
if (kvp.Value > maxi)
{
maxi = kvp.Value;
ans = kvp.Key;
}
}
Console.Write( 'a' );
foreach ( var c in ans)
{
Console.Write(c);
}
}
static void Main( string [] args)
{
string s = "bbbbbaaaaabbabababa" ;
long K = 5;
maximumOccurringString(s, K);
}
}
}
|
Javascript
function maximumOccurringString(s, K) {
let M = {};
let D = [];
for (let i = 0; i < K; i++) {
D.push(s[i]);
}
M[D.join( '' )] = M[D.join( '' )] ? M[D.join( '' )] + 1 : 1;
D.shift();
for (let j = K; j < s.length; j++) {
D.push(s[j]);
M[D.join( '' )] = M[D.join( '' )] ? M[D.join( '' )] + 1 : 1;
D.shift();
}
let maxi = -Infinity;
let ans = [];
for (let it in M) {
if (M[it] >= maxi) {
maxi = M[it];
ans = it.split( '' );
}
}
console.log(ans.join( '' ));
}
let s = "bbbbbaaaaabbabababa" ;
let K = 5;
maximumOccurringString(s, K);
|
Time Complexity: O((N – K)*log(N – K))
Auxiliary Space: O(N – K)
Similar Reads
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
5 min read
Frequency of maximum occurring subsequence in given string
Given a string str of lowercase English alphabets, our task is to find the frequency of occurrence a subsequence of the string which occurs the maximum times. Examples: Input: s = "aba" Output: 2 Explanation: For "aba", subsequence "ab" occurs maximum times in subsequence 'ab' and 'aba'. Input: s =
6 min read
Maximize the minimum length of K palindromic Strings formed from given String
Given a string str of length N, and an integer K, the task is to form K different strings by choosing characters from the given string such that all the strings formed are palindrome and the length of the smallest string among the K strings is maximum possible. Examples: Input: str = "qrsprtps", K =
10 min read
Substring with highest frequency length product
Given a string which contains lower alphabetic characters, we need to find out such a substring of this string whose product of length and frequency in string is maximum among all possible choices of substrings. Examples: Input : String str = âabddabâOutput : 6All unique substring with product of th
15+ min read
Count M-length substrings occurring exactly K times in a string
Given a string S of length N and two integers M and K, the task is to count the number of substrings of length M occurring exactly K times in the string S. Examples: Input: S = "abacaba", M = 3, K = 2Output: 1Explanation: All distinct substrings of length 3 are "aba", "bac", "aca", "cab".Out of all
15+ min read
Minimum cost for constructing the subsequence of length K from given string S
Given a string S consisting of N lowercase English alphabets, and an integer K and, an array cost[] of size 26 denoting the cost of each lowercase English alphabet, the task is to find the minimum cost to construct a subsequence of length K from the characters of the string S. Examples: Input: S = "
11 min read
Count of isogram strings in given array of strings with length at least K
Given an array arr[] containing N strings and an integer K, the task is to find the number of strings which are isograms and at least of length K. Examples: Input: arr[] = {"abcd", "der", "erty"}, K = 4Output: 2Explanation: All given strings are isograms, but only "abcd" and "erty" are of length at
5 min read
Frequency of a Substring in a String
Given an input string and a pattern, the task is to find the frequency of occurrences of the string pattern in a given string. Examples: Input: pattern = "man", string = "dhimanman"Output: 2 Input: pattern = "nn", string = "Banana"Output: 0 Input: pattern = "aa", string = "aaaaa"Output : 4 Recommend
14 min read
Maximum bitwise OR one of any two Substrings of given Binary String
Given a binary string str, the task is to find the maximum possible OR value of any two substrings of the binary string str. Examples: Input: str = "1110010"Output: "1111110"Explanation: On choosing the substring "1110010" and "11100" we get the OR value as "1111110" which is the maximum value. Inpu
8 min read
Maximum repeated frequency of characters in a given string
Given a string S, the task is to find the count of maximum repeated frequency of characters in the given string S.Examples: Input: S = "geeksgeeks" Output: Frequency 2 is repeated 3 times Explanation: Frequency of characters in the given string - {"g": 2, "e": 4, "k": 2, "s": 2} The frequency 2 is r
6 min read