Count of substrings of given string with frequency of each character at most K
Given a string str, the task is to calculate the number of substrings of the given string such that the frequency of each element of the string is almost K.
Examples:
Input: str = “abab”, K = 1
Output: 7
Explanation: The substrings such that the frequency of each character is atmost 1 are “a”, “b”, “a”, “b”, “ab”, “ba” and “ab”.
Input: str[] = “xxyyzzxx”, K = 2
Output: 33
Approach: The given problem can be solved using the two-pointer technique. Iterate through each character of the string in the range [0, N) and maintain the frequency of each character in an unordered map. Create a variable ptr, which stores the index of the starting point of the current window. Initially, the value of ptr is 0. For index i, if the frequency of str[i] is less than or equal to K, add (i – ptr + 1) into the substring count, otherwise, increment the value of ptr until str[i] > K.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int cntSubstr(string str, int K)
{
int N = str.size();
int ans = 0;
int ptr = 0;
unordered_map< char , int > m;
for ( int i = 0; i < N; i++) {
m[str[i]]++;
while (m[str[i]] > K && ptr <= i) {
m[str[ptr]]--;
ptr++;
}
ans += (i - ptr + 1);
}
return ans;
}
int main()
{
string str = "abab" ;
int K = 1;
cout << cntSubstr(str, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int cntSubstr(String str, int K)
{
int N = str.length();
int ans = 0 ;
int ptr = 0 ;
HashMap<Character,
Integer> m = new HashMap<Character,
Integer>();
for ( int i = 0 ; i < N; i++)
{
int count = 0 ;
if (m.containsKey(str.charAt(i)))
{
count = m.get(str.charAt(i));
}
m.put(str.charAt(i), count + 1 );
while (m.get(str.charAt(i)) > K && ptr <= i)
{
m.put(str.charAt(ptr),
m.get(str.charAt(ptr)) - 1 );
ptr++;
}
ans += (i - ptr + 1 );
}
return ans;
}
public static void main(String[] args)
{
String str = "abab" ;
int K = 1 ;
System.out.println(cntSubstr(str, K));
}
}
|
Python3
def cntSubstr( str , K):
N = len ( str )
ans = 0
ptr = 0
m = {}
for i in range (N) :
if ( str [i] in m):
m[ str [i]] + = 1
else :
m[ str [i]] = 1
while (m[ str [i]] > K and ptr < = i):
m[ str [ptr]] - = 1
ptr + = 1
ans + = (i - ptr + 1 )
return ans
str = "abab"
K = 1
print (cntSubstr( str , K))
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
static int cntSubstr( string str, int K)
{
int N = str.Length;
int ans = 0;
int ptr = 0;
Dictionary< char , int > m =
new Dictionary< char , int >();
for ( int i = 0; i < N; i++) {
int count = 0;
if (m.ContainsKey(str[i])) {
count = m[str[i]];
}
m[str[i]] = count + 1;
while (m[str[i]] > K && ptr <= i) {
m[str[ptr]]--;
ptr++;
}
ans += (i - ptr + 1);
}
return ans;
}
public static void Main()
{
string str = "abab" ;
int K = 1;
Console.Write(cntSubstr(str, K));
}
}
|
Javascript
<script>
function cntSubstr(str, K) {
let N = str.length;
let ans = 0;
let ptr = 0;
let m = new Map();
for (let i = 0; i < N; i++) {
if (m.has(str.charAt(i)))
m.set(str[i], m.get(str[i]) + 1);
else
m.set(str[i], 1);
while (m.get(str[i]) > K && ptr <= i) {
m.set(str[ptr], m.get(str[ptr]) - 1);
ptr++;
}
ans += (i - ptr + 1);
}
return ans;
}
let str = "abab" ;
let K = 1;
document.write(cntSubstr(str, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
14 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...