Count of substrings from given Ternary strings containing characters at least once
Last Updated :
19 Jan, 2022
Given string str of size N consisting of only 0, 1, and 2, the task is to find the number of substrings that consists of characters 0, 1, and 2 at least once.
Examples:
Input: str = “0122”
Output: 2
Explanation:
There exists 2 substrings such that the substrings has characters 0, 1, 2 at least once is “012” and “0122”. Therefore, the count of substrings is 2.
Input: S = “00021”
Output: 3
Approach: The given problem can be solved using the Sliding Window Technique, the idea is to make the frequency array of the size 3 that contains the occurrence of 0, 1, and 2. Traverse the given string and update the freq array accordingly, if all 3 indexes in the array are greater than zero then count the minimum of them and increment it into variable count. Follow the steps below to solve the problem:
- Initialize an array freq[] of size 3 to store the frequency of all elements in the array.
- Initialize the variable count as 0 to store the answer and i as 0 to maintain the left pointer.
- Iterate over the range [0, N) using the variable j and perform the following tasks:
- Increase the frequency of the current character str[I] in the array freq[] by 1.
- Traverse in a while loop till freq[0], freq[1], and freq[2] are greater than 0, decrease the frequency of the character at i-th position by 1 and increase the value of i by 1.
- Add the value of i to the variable count.
- After performing the above steps, print the value of count as the answer.
Below is the implementation of the above approach.
C++
#include <iostream>
#include <string>
using namespace std;
int countSubstrings(string& str)
{
int freq[3] = { 0 };
int count = 0;
int i = 0;
for ( int j = 0; j < str.length(); j++) {
freq[str[j] - '0' ]++;
while (freq[0] > 0 && freq[1] > 0 && freq[2] > 0) {
freq[str[i++] - '0' ]--;
}
count += i;
}
return count;
}
int main()
{
string str = "00021" ;
int count = countSubstrings(str);
cout << count;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int countSubstrings(String str)
{
int [] freq = new int [ 3 ];
int count = 0 ;
int i = 0 ;
for ( int j = 0 ;
j < str.length(); j++) {
freq[str.charAt(j) - '0' ]++;
while (freq[ 0 ] > 0 && freq[ 1 ] > 0
&& freq[ 2 ] > 0 ) {
freq[str.charAt(i++) - '0' ]--;
}
count += i;
}
return count;
}
public static void main(String[] args)
{
String str = "00021" ;
System.out.println(
countSubstrings(str));
}
}
|
Python3
def countSubstrings( str ):
freq = [ 0 ] * 3
count = 0
i = 0
for j in range ( 0 , len ( str )):
freq[ ord ( str [j]) - ord ( '0' )] + = 1
while (freq[ 0 ] > 0 and freq[ 1 ] > 0 and freq[ 2 ] > 0 ):
i + = 1
freq[ ord ( str [i]) - ord ( '0' )] - = 1
count + = i
return count
str = "00021"
count = countSubstrings( str )
print (count)
|
C#
using System;
class GFG {
public static int countSubstrings( string str)
{
int [] freq = new int [3];
int count = 0;
int i = 0;
for ( int j = 0;
j < str.Length; j++) {
freq[str[j] - '0' ]++;
while (freq[0] > 0 && freq[1] > 0
&& freq[2] > 0) {
freq[str[i++] - '0' ]--;
}
count += i;
}
return count;
}
public static void Main(String[] args)
{
string str = "00021" ;
Console.Write(countSubstrings(str));
}
}
|
Javascript
<script>
function countSubstrings(str) {
let freq = new Array(3).fill(0)
let count = 0;
let i = 0;
for (let j = 0; j < str.length; j++) {
freq[str.charCodeAt(j) - '0' .charCodeAt(0)]++;
while (freq[0] > 0 && freq[1] > 0 && freq[2] > 0) {
freq[str.charCodeAt(i++) - '0' .charCodeAt(0)]--;
}
count += i;
}
return count;
}
let str = "00021" ;
let count = countSubstrings(str);
document.write(count);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...