Count distinct substrings that contain some characters at most k times
Given a integer k and a string str, the task is to count the number of distinct sub-strings such that each sub-string does not contain some specific characters more than k times. The specific characters are given as another string.
Examples:
Input: str = “ababab”, anotherStr = “bcd”, k = 1
Output: 5
All valid sub-strings are “a”, “b”, “ab”, “ba” and “aba”
Input: str = “acbacbacaa”, anotherStr = “ycb”, k = 2
Output: 8
Approach:
- Store characters of anotherStr in a boolean array of size 256 for quick lookip
- Traverse through all substrings of given string. For every substring, keep the count of illegal characters in anotherStr.
- If the count of these characters exceeds the value of k then break out of the inner loop.
- Else, store this sub-string in an hash table to keep distinct substrings.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 256;
int countSubStrings(string s, string anotherStr, int k)
{
bool illegal[MAX_CHAR] = { false };
for ( int i = 0; i < anotherStr.size(); i++)
illegal[anotherStr[i]] = true ;
unordered_set<string> us;
for ( int i = 0; i < s.size(); ++i) {
string ss = "" ;
int count = 0;
for ( int j = i; j < s.size(); ++j) {
if (illegal[s[j]])
++count;
ss = ss + s[j];
if (count <= k) {
us.insert(ss);
}
else
break ;
}
}
return us.size();
}
int main()
{
string str = "acbacbacaa" ;
string anotherStr = "abcdefghijklmnopqrstuvwxyz" ;
int k = 2;
cout << countSubStrings(str, anotherStr, k);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int MAX_CHAR = 256 ;
static int countSubStrings(String s, String anotherStr, int k)
{
boolean illegal[] = new boolean [MAX_CHAR];
for ( int i = 0 ; i < anotherStr.length(); i++)
{
illegal[anotherStr.charAt(i)] = true ;
}
HashSet<String> us = new HashSet<String>();
for ( int i = 0 ; i < s.length(); ++i)
{
String ss = "" ;
int count = 0 ;
for ( int j = i; j < s.length(); ++j)
{
if (illegal[s.charAt(j)])
{
++count;
}
ss = ss + s.charAt(j);
if (count <= k)
{
us.add(ss);
}
else
{
break ;
}
}
}
return us.size();
}
public static void main(String[] args)
{
String str = "acbacbacaa" ;
String anotherStr = "abcdefghijklmnopqrstuvwxyz" ;
int k = 2 ;
System.out.println(countSubStrings(str, anotherStr, k));
}
}
|
Python3
MAX_CHAR = 256
def countSubStrings(s, anotherStr, k) :
illegal = [ False ] * MAX_CHAR
for i in range ( len (anotherStr)) :
illegal[ ord (anotherStr[i])] = True
us = set ()
for i in range ( len (s)) :
ss = ""
count = 0
for j in range (i, len (s)) :
if (illegal[ ord (s[j])]) :
count + = 1
ss = ss + s[j]
if (count < = k) :
us.add(ss)
else :
break
return len (us)
if __name__ = = "__main__" :
string = "acbacbacaa"
anotherStr = "abcdefghijklmnopqrstuvwxyz"
k = 2
print (countSubStrings(string,
anotherStr, k))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int MAX_CHAR = 256;
static int countSubStrings(String s,
String anotherStr, int k)
{
bool []illegal = new bool [MAX_CHAR];
for ( int i = 0; i < anotherStr.Length; i++)
{
illegal[anotherStr[i]] = true ;
}
HashSet<String> us = new HashSet<String>();
for ( int i = 0; i < s.Length; ++i)
{
String ss = "" ;
int count = 0;
for ( int j = i; j < s.Length; ++j)
{
if (illegal[s[j]])
{
++count;
}
ss = ss + s[j];
if (count <= k)
{
us.Add(ss);
}
else
{
break ;
}
}
}
return us.Count;
}
public static void Main()
{
String str = "acbacbacaa" ;
String anotherStr = "abcdefghijklmnopqrstuvwxyz" ;
int k = 2;
Console.WriteLine(countSubStrings(str, anotherStr, k));
}
}
|
Javascript
<script>
let MAX_CHAR = 256;
function countSubStrings( s, anotherStr, k)
{
let illegal = [];
for (let i = 0;i<256;i++)
illegal.push( false );
for (let i = 0; i < anotherStr.length; i++)
illegal[anotherStr[i]] = true ;
let us = new Set();
for (let i = 0; i < s.length; ++i) {
let ss = "" ;
let count = 0;
for (let j = i; j < s.length; ++j) {
if (illegal[s[j]])
++count;
ss = ss + s[j];
if (count <= k) {
us.add(ss);
}
else
break ;
}
}
return us.size;
}
let str = "acbacbacaa" ;
let anotherStr = "abcdefghijklmnopqrstuvwxyz" ;
let k = 2;
document.write(countSubStrings(str, anotherStr, k));
</script>
|
Time Complexity: O(max(n2, m), where n and m are the length of string “str” and “anotherstr” respectively,
Auxiliary Space: O(max(n2, MAX_CHAR))
Last Updated :
09 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...