Count subsequences 01 in string generated by concatenation of given numeric string K times
Last Updated :
18 Nov, 2021
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to calculate the number of
// subsequences of "01"
int countSubsequence(string S, int N,
int K)
{
// Store count of 0's and 1's
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++;
}
// Count of subsequences without
// concatenation
int B1 = 0;
for (int i = 0; i < N; i++) {
if (S[i] == '1')
B1++;
else if (S[i] == '0')
C = C + (C1 - B1);
}
// Case 1
int ans = C * K;
// Case 2
ans += (C1 * C0 * (((K) * (K - 1)) / 2));
// Return the total count
return ans;
}
// Driver Code
int main()
{
string S = "230013110087";
int K = 2;
int N = S.length();
cout << countSubsequence(S, N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to calculate the number of
// subsequences of "01"
static int countSubsequence(String S, int N, int K)
{
// Store count of 0's and 1's
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++;
}
// Count of subsequences without
// concatenation
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);
}
// Case 1
int ans = C * K;
// Case 2
ans += (C1 * C0 * (((K) * (K - 1)) / 2));
// Return the total count
return ans;
}
// Driver Code
public static void main(String[] args)
{
String S = "230013110087";
int K = 2;
int N = S.length();
System.out.println(countSubsequence(S, N, K));
}
}
// This code is contributed by Potta Lokesh
Python3
# python program for the above approach
# Function to calculate the number of
# subsequences of "01"
def countSubsequence(S, N, K):
# Store count of 0's and 1's
C = 0
C1 = 0
C0 = 0
for i in range(0, N):
if (S[i] == '1'):
C1 += 1
elif (S[i] == '0'):
C0 += 1
# Count of subsequences without
# concatenation
B1 = 0
for i in range(0, N):
if (S[i] == '1'):
B1 += 1
elif (S[i] == '0'):
C = C + (C1 - B1)
# Case 1
ans = C * K
# Case 2
ans += (C1 * C0 * (((K) * (K - 1)) // 2))
# Return the total count
return ans
# Driver Code
if __name__ == "__main__":
S = "230013110087"
K = 2
N = len(S)
print(countSubsequence(S, N, K))
# This code is contributed by rakeshsahni
C#
// C# implementation for the above approach
using System;
class GFG
{
// Function to calculate the number of
// subsequences of "01"
static int countSubsequence(string S, int N, int K)
{
// Store count of 0's and 1's
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++;
}
// Count of subsequences without
// concatenation
int B1 = 0;
for (int i = 0; i < N; i++) {
if (S[i] == '1')
B1++;
else if (S[i] == '0')
C = C + (C1 - B1);
}
// Case 1
int ans = C * K;
// Case 2
ans += (C1 * C0 * (((K) * (K - 1)) / 2));
// Return the total count
return ans;
}
// Driver Code
public static void Main()
{
string S = "230013110087";
int K = 2;
int N = S.Length;
Console.Write(countSubsequence(S, N, K));
}
}
// This code is contributed by sanjoy_62.
JavaScript
<script>
// Javascript program for the above approach
// Function to calculate the number of
// subsequences of "01"
function countSubsequence(S, N, K) {
// Store count of 0's and 1's
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++;
}
// Count of subsequences without
// concatenation
let B1 = 0;
for (let i = 0; i < N; i++) {
if (S[i] == "1") B1++;
else if (S[i] == "0") C = C + (C1 - B1);
}
// Case 1
let ans = C * K;
// Case 2
ans += C1 * C0 * ((K * (K - 1)) / 2);
// Return the total count
return ans;
}
// Driver Code
let S = "230013110087";
let K = 2;
let N = S.length;
document.write(countSubsequence(S, N, K));
// This code is contributed by gfgking.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Count binary Strings that does not contain given String as Subsequence Given a number N and string S, count the number of ways to create a binary string (containing only '0' and '1') of size N such that it does not contain S as a subsequence. Examples: Input: N = 3, S = "10".Output: 4Explanation: There are 8 strings possible to fill 3 positions with 0's or 1's. {"000",
15+ min read
Generate a string whose all K-size substrings can be concatenated to form the given string Given a string str of size N and an integer K, the task is to generate a string whose substrings of size K can be concatenated to form the given string. Examples: Input: str = "abbaaa" K = 2 Output: abaa Explanation: All substring of size 2 of parent string "abaa" are "ab", "ba" and "aa". After conc
5 min read
Number of subsequences as "ab" in a string repeated K times Given a String S, consider a new string formed by repeating the S exactly K times. We need find the number of subsequences as âabâ in the newly formed string. Examples : Input : S = "abcb" K = 2 Output : 6 Here, Given string is repeated 2 times and we get a new string "abcbabcb" Below are 6 occurren
10 min read
Generate Binary String with equal number of 01 and 10 Subsequence Given an integer N (N > 2), the task is to generate a binary string of size N that consists of equal numbers of "10" & "01" subsequences and also the string should contain at least one '0' and one '1' Note: If multiple such strings exist, print any. Examples: Input: 4Output: 0110Explanation :
7 min read
Construct a string that has exactly K subsequences from given string Given a string str and an integer K, the task is to find a string S such that it has exactly K subsequences of given string str. Examples: Input: str = "gfg", K = 10 Output: gggggffg Explanation: There are 10 possible subsequence of the given string "gggggffg". They are: 1. gggggffg 2. gggggffg 3. g
7 min read