Longest sub-string having frequency of each character less than equal to k
Given a string str of length n. The problem is to find the length of the longest sub-string in str having frequency of each character less than equal to the given value k.
Examples :
Input : str = "babcaag", k = 1
Output : 3
abc and bca are the two longest
sub-strings having frequency of each character
in them less than equal to '1'.
Input : str = "geeksforgeeks", k = 2
Output : 10
Approach: Create an array freq[] of size 26 implemented as hash table to store the frequency of each character of str. Initialize all of its indexes with value ‘0’. Length of the string is n. Now implement the following algorithm.
longSubstring(str, k)
Initialize start = 0
Initialize maxLen = 0
Declare ch
for i = 0 to n-1
ch = str[i]
freq[ch - 'a']++
if k < freq[ch - 'a'] then
if maxLen < (i - start) then
maxLen = i - start
while (k < freq[ch - 'a'])
freq[str[start] - 'a']--
start++
if maxLen < (n - start) then
maxLen = n - start
return maxLen
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
#define SIZE 26
int longSubstring(string str, int k)
{
int freq[SIZE];
memset (freq, 0, sizeof (freq));
int start = 0;
int maxLen = 0;
char ch;
int n = str.size();
for ( int i = 0; i < n; i++)
{
ch = str[i];
freq[ch - 'a' ]++;
if (freq[ch - 'a' ] > k)
{
if (maxLen < (i - start))
maxLen = i - start;
while (freq[ch - 'a' ] > k)
{
freq[str[start] - 'a' ]--;
start++;
}
}
}
if (maxLen < (n - start))
maxLen = n - start;
return maxLen;
}
int main()
{
string str = "babcaag" ;
int k = 1;
cout << "Length = "
<< longSubstring(str, k);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG{
public final static int SIZE = 26 ;
public static int longSubstring(String str1,
int k)
{
int [] freq = new int [SIZE];
char [] str = str1.toCharArray();
int start = 0 ;
int maxLen = 0 ;
char ch;
int n = str1.length();
for ( int i = 0 ; i < n; i++)
{
ch = str[i];
freq[ch - 'a' ]++;
if (freq[ch - 'a' ] > k)
{
if (maxLen < (i - start))
maxLen = i - start;
while (freq[ch - 'a' ] > k)
{
freq[str[start] - 'a' ]--;
start++;
}
}
}
if (maxLen < (n - start))
maxLen = n - start;
return maxLen;
}
public static void main(String argc[])
{
String str = "babcaag" ;
int k = 1 ;
System.out.println( "Length = " +
longSubstring(str, k));
}
}
|
Python3
import numpy as np
SIZE = 26
def longSub( str , k):
freq = np.zeros( 26 , dtype = np. int )
start = 0
maxLen = 0
n = len ( str )
for i in range ( 0 , n):
ch = str [i]
freq[ ord (ch) - ord ( 'a' ) ] + = 1
if (freq[ ord (ch) - ord ( 'a' )] > k):
if (maxLen < (i - start)):
maxLen = i - start
while (freq[ ord (ch) - ord ( 'a' )] > k):
freq[ ord ( str [start]) - ord ( 'a' )] - = 1
start = start + 1
if (maxLen < (n - start)):
maxLen = n - start
return maxLen;
str = "babcaag"
k = 1
print ( "Length =" , longSub( str , k))
|
C#
using System;
class GfG{
public static int SIZE = 26;
public static int longSubstring(String str1,
int k)
{
int []freq = new int [SIZE];
char []str = str1.ToCharArray();
int start = 0;
int maxLen = 0;
char ch;
int n = str1.Length;
for ( int i = 0; i < n; i++)
{
ch = str[i];
freq[ch - 'a' ]++;
if (freq[ch - 'a' ] > k)
{
if (maxLen < (i - start))
maxLen = i - start;
while (freq[ch - 'a' ] > k)
{
freq[str[start] - 'a' ]--;
start++;
}
}
}
if (maxLen < (n - start))
maxLen = n - start;
return maxLen;
}
public static void Main()
{
String str = "babcaag" ;
int k = 1;
Console.Write( "Length = " +
longSubstring(str, k));
}
}
|
PHP
<?php
$SIZE = 26;
function longSubstring( $str , $k )
{
global $SIZE ;
$freq = array ();
for ( $i = 0; $i < $SIZE ; $i ++)
$freq [ $i ] = 0;
$start = 0;
$maxLen = 0;
$ch = '' ;
$n = strlen ( $str );
for ( $i = 0; $i < $n ; $i ++)
{
$ch = $str [ $i ];
$freq [ord( $ch ) -
ord( 'a' )]++;
if ( $freq [ord( $ch ) -
ord( 'a' )] > $k )
{
if ( $maxLen < ( $i - $start ))
$maxLen = $i - $start ;
while ( $freq [ord( $ch ) -
ord( 'a' )] > $k )
{
$freq [ord( $str [ $start ]) -
ord( 'a' )]--;
$start ++;
}
}
}
if ( $maxLen < ( $n - $start ))
$maxLen = $n - $start ;
return $maxLen ;
}
$str = "babcaag" ;
$k = 1;
echo ( "Length = " .
longSubstring( $str , $k ));
?>
|
Javascript
<script>
var SIZE = 26;
function longSubstring(str, k)
{
var freq = Array(SIZE).fill(0);
var start = 0;
var maxLen = 0;
var ch;
var n = str.length;
for ( var i = 0; i < n; i++)
{
ch = str[i];
freq[ch.charCodeAt(0) -
'a' .charCodeAt(0)]++;
if (freq[ch.charCodeAt(0) -
'a' .charCodeAt(0)] > k)
{
if (maxLen < (i - start))
maxLen = i - start;
while (freq[ch.charCodeAt(0) -
'a' .charCodeAt(0)] > k)
{
freq[str[start].charCodeAt(0) -
'a' .charCodeAt(0)]--;
start++;
}
}
}
if (maxLen < (n - start))
maxLen = n - start;
return maxLen;
}
var str = "babcaag" ;
var k = 1;
document.write( "Length = "
+ longSubstring(str, k));
</script>
|
Time Complexity: O(n).
Auxiliary Space: O(1).
Because of the while loop the complexity might seem quadratic but if we look closely the inner while loop will traverse the string single time only.”
Last Updated :
29 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...