Count Substrings with all Unique Digits in Range [0, K]
Last Updated :
23 Dec, 2023
Given a string S of length N (1 <= N <= 107) and a positive integer K, where the string contains only numbers in the range [0, 9], the task is to determine the count of substrings that include at least one occurrence of all the unique digits within the specified range [0, K].
Example:
Input: S = “4250163456”, K = 3
Output: 5
Explanation: The substrings containing at least one occurrence of all first three unique digits in the range [0, 3] are “4250163“, “42501634“, “425016345″, “4250163456″, “250163“, “250163“, “2501634″, “25016345″, “250163456″.
Input: S = “978623”, K = 5
Output: 0
Count Substrings with All Unique Digits in Range [0, K] using Hashing:
Begin with two pointers, start and end, both at the string’s beginning. For each start, find the first valid substring starting at start and ending at end. Since this is the first valid substring from start, we can also include the remaining characters from the end index, meeting the minimum validity criteria. There will be (N – end) valid substrings for this start index. Add up all these counts to the result and return it.
Step-by-step approach:
- Create a character frequency map unmap and variables start, end, n (string size), and result to 0.
- Create through the string s until the end is less than its size n.
- If the character at end is in the range [0, K], increment its frequency count in unmap.
- Check if unmap contains K+1 unique characters. If it does, initiate a loop until unmap contains K unique characters:
- Increment result by the count of valid substrings, which is (n – end).
- Decrement the frequency count of the character at start if it’s in the range [0, K].
- If the frequency count becomes 0, remove that character from unmap.
- Increment start.
- Increment end and continue.
- Return result as the final count of valid substrings.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int countSubstrings(string s, int K)
{
unordered_map< char , int > unmap;
int start = 0, end = 0, n = s.size(), result = 0;
while (end < n) {
if (s[end] <= (K + '0' ))
unmap[s[end]]++;
if (unmap.size() == K + 1) {
while (unmap.size() == K + 1) {
result += n - end;
if (s[start] <= (K + '0' ))
unmap[s[start]]--;
if (unmap[s[start]] == 0)
unmap.erase(s[start]);
start++;
}
end++;
}
else {
end++;
}
}
return result;
}
int main()
{
string S = "4250163456";
int K = 3;
cout << countSubstrings(S, K);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
public class SubstringCount {
static int countSubstrings(String s, int K)
{
Map<Character, Integer> map = new HashMap<>();
int start = 0 , end = 0 , n = s.length(), result = 0 ;
while (end < n) {
if (s.charAt(end) <= (K + '0' )) {
map.put(s.charAt(end),
map.getOrDefault(s.charAt(end), 0 )
+ 1 );
}
if (map.size() == K + 1 ) {
while (map.size() == K + 1 ) {
result += n - end;
if (s.charAt(start) <= (K + '0' )) {
map.put(s.charAt(start),
map.get(s.charAt(start))
- 1 );
if (map.get(s.charAt(start)) == 0 ) {
map.remove(s.charAt(start));
}
}
start++;
}
end++;
}
else {
end++;
}
}
return result;
}
public static void main(String[] args)
{
String S = "4250163456" ;
int K = 3 ;
System.out.println(countSubstrings(S, K));
}
}
|
Python3
def countSubstrings(s, K):
unmap = {}
start, end, n, result = 0 , 0 , len (s), 0
while end < n:
if s[end] < = chr (K + ord ( '0' )):
unmap[s[end]] = unmap.get(s[end], 0 ) + 1
if len (unmap) = = K + 1 :
while len (unmap) = = K + 1 :
result + = n - end
if s[start] < = chr (K + ord ( '0' )):
unmap[s[start]] = unmap.get(s[start], 0 ) - 1
if unmap[s[start]] = = 0 :
del unmap[s[start]]
start + = 1
end + = 1
else :
end + = 1
return result
S = "4250163456"
K = 3
print (countSubstrings(S, K))
|
C#
using System;
using System.Collections.Generic;
public class SubstringCount {
static int CountSubstrings( string s, int K)
{
Dictionary< char , int > map
= new Dictionary< char , int >();
int start = 0, end = 0, n = s.Length, result = 0;
while (end < n) {
if (s[end] <= (K + '0' )) {
map[s[end]]
= map.GetValueOrDefault(s[end], 0) + 1;
}
if (map.Count == K + 1) {
while (map.Count == K + 1) {
result += n - end;
if (s[start] <= (K + '0' )) {
map[s[start]] = map[s[start]] - 1;
if (map[s[start]] == 0) {
map.Remove(s[start]);
}
}
start++;
}
end++;
}
else {
end++;
}
}
return result;
}
public static void Main( string [] args)
{
string S = "4250163456" ;
int K = 3;
Console.WriteLine(CountSubstrings(S, K));
}
}
|
Javascript
function GFG(s, K) {
const unmap = new Map();
let start = 0,
end = 0,
n = s.length,
result = 0;
while (end < n) {
if (s[end] <= String(K)) {
unmap.set(s[end], (unmap.get(s[end]) || 0) + 1);
}
if (unmap.size === K + 1) {
while (unmap.size === K + 1) {
result += n - end;
if (s[start] <= String(K)) {
unmap.set(s[start], unmap.get(s[start]) - 1);
}
if (unmap.get(s[start]) === 0) {
unmap. delete (s[start]);
}
start++;
}
end++;
} else {
end++;
}
}
return result;
}
const S = "4250163456" ;
const K = 3;
console.log(GFG(S, K));
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...