Count subsequences 01 in string generated by concatenation of given numeric string K times
Given a string S and a positive integer K, the task is to find the number of subsequences “01” in the string generated by concatenation of the given numeric string S K times.
Examples:
Input: S = “0171”, K = 2
Output: 6
Explanation:
The string formed by concatenation of S, K number of times is “01710171”. There are total 6 possible subsequences which are marked as bold = {“01710171″, “01710171″, “01710171″, “01710171“, “01710171″, “01710171“}.
Input: S = “230013110087”, K = 2
Output: 24
Naive Approach: The simplest approach to solve the given problem is to generate the resultant string by concatenating S, K number of times and then find all possible pairs (i, j) from the string such that (i < j) and S[i] = 0 and S[j] = 1.
Time Complexity: O((N*K)2)
Auxiliary Space: O(N*K)
Efficient Approach: The task can also be optimized by observing the following 2 Cases:
- Case 1: Substring “01” strictly inside each occurrence of S in P. Let suppose C be the count of occurrences of “01” in S, then in P it would be C*K.
- Case 2: When ‘0‘ lies inside at ith occurrence of S and ‘1‘ lies inside some jth occurrence to form a subsequence “01” such that i < j, then finding the number of occurrences of “01” will be the same as choosing the two strings or occurrence of strings in P given by ((K)*(K – 1))/2. Let that value be Si and Sj and multiplying it by the number of occurrences of ‘0’ in Si(denoted by cnt0) and a number of occurrences of ‘1’ in Sj(denoted by cnt1) gives the number of subsequences of “01”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countSubsequence(string S, int N,
int K)
{
int C = 0, C1 = 0, C0 = 0;
for ( int i = 0; i < N; i++) {
if (S[i] == '1' )
C1++;
else if (S[i] == '0' )
C0++;
}
int B1 = 0;
for ( int i = 0; i < N; i++) {
if (S[i] == '1' )
B1++;
else if (S[i] == '0' )
C = C + (C1 - B1);
}
int ans = C * K;
ans += (C1 * C0 * (((K) * (K - 1)) / 2));
return ans;
}
int main()
{
string S = "230013110087" ;
int K = 2;
int N = S.length();
cout << countSubsequence(S, N, K);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countSubsequence(String S, int N, int K)
{
int C = 0 , C1 = 0 , C0 = 0 ;
for ( int i = 0 ; i < N; i++) {
if (S.charAt(i) == '1' )
C1++;
else if (S.charAt(i) == '0' )
C0++;
}
int B1 = 0 ;
for ( int i = 0 ; i < N; i++) {
if (S.charAt(i) == '1' )
B1++;
else if (S.charAt(i) == '0' )
C = C + (C1 - B1);
}
int ans = C * K;
ans += (C1 * C0 * (((K) * (K - 1 )) / 2 ));
return ans;
}
public static void main(String[] args)
{
String S = "230013110087" ;
int K = 2 ;
int N = S.length();
System.out.println(countSubsequence(S, N, K));
}
}
|
Python3
def countSubsequence(S, N, K):
C = 0
C1 = 0
C0 = 0
for i in range ( 0 , N):
if (S[i] = = '1' ):
C1 + = 1
elif (S[i] = = '0' ):
C0 + = 1
B1 = 0
for i in range ( 0 , N):
if (S[i] = = '1' ):
B1 + = 1
elif (S[i] = = '0' ):
C = C + (C1 - B1)
ans = C * K
ans + = (C1 * C0 * (((K) * (K - 1 )) / / 2 ))
return ans
if __name__ = = "__main__" :
S = "230013110087"
K = 2
N = len (S)
print (countSubsequence(S, N, K))
|
C#
using System;
class GFG
{
static int countSubsequence( string S, int N, int K)
{
int C = 0, C1 = 0, C0 = 0;
for ( int i = 0; i < N; i++) {
if (S[i] == '1' )
C1++;
else if (S[i] == '0' )
C0++;
}
int B1 = 0;
for ( int i = 0; i < N; i++) {
if (S[i] == '1' )
B1++;
else if (S[i] == '0' )
C = C + (C1 - B1);
}
int ans = C * K;
ans += (C1 * C0 * (((K) * (K - 1)) / 2));
return ans;
}
public static void Main()
{
string S = "230013110087" ;
int K = 2;
int N = S.Length;
Console.Write(countSubsequence(S, N, K));
}
}
|
Javascript
<script>
function countSubsequence(S, N, K) {
let C = 0,
C1 = 0,
C0 = 0;
for (let i = 0; i < N; i++) {
if (S[i] == "1" ) C1++;
else if (S[i] == "0" ) C0++;
}
let B1 = 0;
for (let i = 0; i < N; i++) {
if (S[i] == "1" ) B1++;
else if (S[i] == "0" ) C = C + (C1 - B1);
}
let ans = C * K;
ans += C1 * C0 * ((K * (K - 1)) / 2);
return ans;
}
let S = "230013110087" ;
let K = 2;
let N = S.length;
document.write(countSubsequence(S, N, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
18 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...