Given a string str and an integer K, the task is to find the length of the longest sub-string S such that every character in S appears at least K times.
Examples:
Input: str = “aabbba”, K = 3
Output: 6
Explanation:
In substring aabbba, each character repeats at least k times and its length is 6.Input: str = “ababacb”, K = 3
Output: 0
Explanation:
There is no substring where each character repeats at least k times.
Naive Approach: We have discussed the Naive Approach in the previous post.
Approach: In this post, we will discuss the approach using Divide and Conquer technique and Recursion. Below are the steps:
- Store the frequency of each characters of the given string in a frequency array of size 26.
- Initialize two variables start with 0 and end which is the length of the string str.
- Iterate over the string from start to end and count the number of times each character repeats and store it in an array.
- If any character repeats less than K times, then Divide the string into two halves. If i is the index of the string where we found that the string[i] repeats less than K times, then we divide the string into two halves from start to i and i + 1 to end.
- Recursively call for the two halves in the above steps i.e., from start to i and i + 1 to end separately and repeat the Step 2 and 3 and return the maximum of the two values reutnr by the above recursive call.
- If all the characters between start and end is repeated at least K times, then the answer is end – start.
Below is the implementation of above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>usingnamespacestd;// Function to find the longest substringintlongestSubstring(intstart,intend,string s,intk){intleft, right;// Array for counting the number of// times each character repeats// count the number of times each// character repeats from start to endintcount[26] = { 0 };// Store the frequency from s[start...end]for(inti = start; i < end; i++) {count[s[i] -'a'] += 1;}// Iterate from [start, end]for(inti = start; i < end; i++) {if(count[s[i] -'a'] < k) {// Recursive call for left subpartleft = longestSubstring(start,i,s,k);// Recursive call for right subpartright = longestSubstring(i + 1,end,s,k);// Return maximum of left & rightreturnmax(left, right);}}// If all the characters are repeated// at least k timesreturnend - start;}// Driver Codeintmain(){// Given String strstring str ="aabbba";intk = 3;// Function Callcout << longestSubstring(0, str.length(),str, k)<< endl;return0;}Java
// Java program for the above approachimportjava.util.*;classGFG{// Function to find the longest subStringstaticintlongestSubString(intstart,intend,String s,intk){intleft, right;// Array for counting the number of// times each character repeats// count the number of times each// character repeats from start to endintcount[] =newint[26];// Store the frequency from s[start...end]for(inti = start; i < end; i++){count[s.charAt(i) -'a'] +=1;}// Iterate from [start, end]for(inti = start; i < end; i++){if(count[s.charAt(i) -'a'] < k){// Recursive call for left subpartleft = longestSubString(start, i,s, k);// Recursive call for right subpartright = longestSubString(i +1, end,s, k);// Return maximum of left & rightreturnMath.max(left, right);}}// If all the characters are repeated// at least k timesreturnend - start;}// Driver Codepublicstaticvoidmain(String[] args){// Given String strString str ="aabbba";intk =3;// Function CallSystem.out.print(longestSubString(0, str.length(),str, k) +"\n");}}// This code is contributed by Amit KatiyarPython3
# Python3 program for the above approach# Function to find the longest substringdeflongestSubString(start, end, s, k):# List for counting the number of# times each character repeats# count the number of times each# chracter repeats from start to endcount=[0foriinrange(26)]# Store the frequency from s[start...end]foriinrange(start, end):count[ord(s[i])-ord('a')]+=1# Iterate from [start, end]foriinrange(start, end):if(count[ord(s[i])-ord('a')] < k):# Recursive call for left subpartleft=longestSubString(start, i,s, k)# Recursive call for right subpartright=longestSubString(i+1, end,s, k)# Return maximum of left & rightreturnmax(left, right)# If all the characters are repeated# at least k timesreturnend-start# Driver Code# Given String strstr="aabbba"k=3# Function callprint(longestSubString(0,len(str),str, k))# This code is contributed by dadimadhavC#
// C# program for the above approachusingSystem;classGFG{// Function to find the longest subStringstaticintlongestSubString(intstart,intend,strings,intk){intleft, right;// Array for counting the number of// times each character repeats// count the number of times each// character repeats from start to endint[]count =newint[26];// Store the frequency from s[start...end]for(inti = start; i < end; i++){count[s[i] -'a'] += 1;}// Iterate from [start, end]for(inti = start; i < end; i++){if(count[s[i] -'a'] < k){// Recursive call for left subpartleft = longestSubString(start, i,s, k);// Recursive call for right subpartright = longestSubString(i + 1, end,s, k);// Return maximum of left & rightreturnMath.Max(left, right);}}// If all the characters are repeated// at least k timesreturnend - start;}// Driver CodepublicstaticvoidMain(string[] args){// Given String strstringstr ="aabbba";intk = 3;// Function callConsole.Write(longestSubString(0, str.Length,str, k) +"\n");}}// This code is contributed by rutvik_56Output:6
Time Complexity: O(N*log2N)
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


