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 = 5Output:ababaExplanation:

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} ocuurs 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 = 5Output:agoodExplanation:

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.

**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++

`// C++ program for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `ll = ` `long` `long` `int` `; ` `using` `namespace` `std; ` ` ` `// Function that generates substring ` `// of length K that occurs maximum times ` `void` `maximumOccurringString(string s, ll K) ` `{ ` ` ` `// Store the frequency of substrings ` ` ` `map<deque<` `char` `>, ll> M; ` ` ` ` ` `ll i; ` ` ` ` ` `// Deque to maintain substrings ` ` ` `// window size K ` ` ` `deque<` `char` `> D; ` ` ` ` ` `for` `(i = 0; i < K; i++) { ` ` ` `D.push_back(s[i]); ` ` ` `} ` ` ` ` ` `// Update the frequency of the ` ` ` `// first substring in the Map ` ` ` `M[D]++; ` ` ` ` ` `// Remove the first character of ` ` ` `// the previous K length substring ` ` ` `D.pop_front(); ` ` ` ` ` `// Traverse the string ` ` ` `for` `(ll j = i; j < s.size(); j++) { ` ` ` ` ` `// Insert the current character ` ` ` `// as last character of ` ` ` `// current substring ` ` ` `D.push_back(s[j]); ` ` ` ` ` `M[D]++; ` ` ` ` ` `// Pop the first character of ` ` ` `// previous K length substring ` ` ` `D.pop_front(); ` ` ` `} ` ` ` ` ` `ll maxi = INT_MIN; ` ` ` ` ` `deque<` `char` `> ans; ` ` ` ` ` `// Find the substring that occurs ` ` ` `// maximum number of times ` ` ` `for` `(` `auto` `it : M) { ` ` ` `if` `(it.second > maxi) { ` ` ` `maxi = it.second; ` ` ` `ans = it.first; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print the substring ` ` ` `for` `(ll i = 0; i < ans.size(); i++) { ` ` ` `cout << ans[i]; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given string ` ` ` `string s = ` `"bbbbbaaaaabbabababa"` `; ` ` ` ` ` `// Given K size of substring ` ` ` `ll K = 5; ` ` ` ` ` `// Function Call ` ` ` `maximumOccurringString(s, K); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` `from` `collections ` `import` `deque, Counter, defaultdict ` `import` `sys ` ` ` `# Function that generates substring ` `# of length K that occurs maximum times ` `def` `maximumOccurringString(s, K): ` ` ` ` ` `# Store the frequency of substrings ` ` ` `M ` `=` `{} ` ` ` ` ` `# Deque to maintain substrings ` ` ` `# window size K ` ` ` `D ` `=` `deque() ` ` ` ` ` `for` `i ` `in` `range` `(K): ` ` ` `D.append(s[i]) ` ` ` ` ` `# Update the frequency of the ` ` ` `# first subin the Map ` ` ` `# E="".join(list(D ` ` ` `M[` `str` `("".join(` `list` `(D)))] ` `=` `M.get( ` ` ` `str` `("".join(` `list` `(D))), ` `0` `) ` `+` `1` ` ` ` ` `# Remove the first character of ` ` ` `# the previous K length substring ` ` ` `D.popleft() ` ` ` ` ` `# Traverse the string ` ` ` `for` `j ` `in` `range` `(i, ` `len` `(s)): ` ` ` ` ` `# Insert the current character ` ` ` `# as last character of ` ` ` `# current substring ` ` ` `D.append(s[j]) ` ` ` ` ` `M[` `str` `("".join(` `list` `(D)))] ` `=` `M.get( ` ` ` `str` `("".join(` `list` `(D))), ` `0` `) ` `+` `1` ` ` ` ` `# Pop the first character of ` ` ` `# previous K length substring ` ` ` `D.popleft() ` ` ` ` ` `maxi ` `=` `-` `sys.maxsize ` `-` `1` ` ` ` ` `ans ` `=` `deque() ` ` ` ` ` `# Find the subthat occurs ` ` ` `# maximum number of times ` ` ` `# print(M) ` ` ` `for` `it ` `in` `M: ` ` ` ` ` `# print(it[0]) ` ` ` `if` `(M[it] >` `=` `maxi): ` ` ` `maxi ` `=` `M[it] ` ` ` ` ` `# print(maxi) ` ` ` `ans ` `=` `it ` ` ` ` ` `# Print the substring ` ` ` `for` `i ` `in` `range` `(` `len` `(ans)): ` ` ` `print` `(ans[i], end ` `=` `"") ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `# Given string ` ` ` `s ` `=` `"bbbbbaaaaabbabababa"` ` ` ` ` `# Given K size of substring ` ` ` `K ` `=` `5` ` ` ` ` `# Function call ` ` ` `maximumOccurringString(s, K) ` ` ` `# This code is contributed by mohit kumar 29` |

*chevron_right*

*filter_none*

**Output:**

ababa

**Time Complexity:** O((N – K)*log(N – K))**Auxiliary Space:** O(N – K)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Maximum length substring with highest frequency in a string
- Maximum difference between frequency of two elements such that element having greater frequency is also greater
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
- Maximum length substring having all same characters after k changes
- Substring with highest frequency length product
- Minimum length of substring whose rotation generates a palindromic substring
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Length of smallest substring of a given string which contains another string as subsequence
- Find the length of the longest subsequence with first K alphabets having same frequency
- Length of longest subarray having frequency of every element equal to K
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Frequency of a substring in a string using pthread
- Frequency of a substring in a string | Set 2
- Frequency of a substring in a string

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.