Open In App

Minimize count of 0s in Binary String by changing K-size substring to 1s at most Q times

Last Updated : 07 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a binary string S having N characters and two integers K and Q, the task is to find the minimum number of remaining zeroes after at most Q operations, such that in each operation, choose any substring of size at most K and change all its elements to 1.

Example: 

Input: S = 000111, K = 2, Q = 1
Output: 1
Explanation:
Choose the substring S[0, 1] and change all the characters to 1. The final string will be 110111 that has 1 zero remaining which is the minimum possible. 

Input: S = 0100010110, K = 3, Q = 2
Output: 2

Approach 1: Memoisation 

We can define dp[i][j] as the minimum number of zeroes in S[0:i] after j operations. We can then derive the recurrence relation using the following steps:

  1.  If K = 0 or Q = 0, we cannot perform any operations, so the answer is simply the number of zeroes in the original string.
  2.  If Q is large enough such that we can perform operations on all substrings of length at most K, then we can simply set the answer to 0.
  3.  Initialize dp[0][j] = 0 for all j, since there are no characters in S[0:0] and we have not performed any operations yet.
  4.  Initialize dp[i][0] = dp[i-1][0] + (S[i-1] == ‘0’) for all i, since the minimum number of zeroes in S[0:i] after 0 operations is simply the minimum number of zeroes in S[0:i-1] plus 1 if the i-th character is ‘0’.
  5.  For all i in the range [1, N] and for all j in the range [1, Q], we can use the following recurrence relation to compute dp[i][j]:
 dp[i][j] = min(dp[i-k][j-1], dp[i-1][j] + (S[i-1] == '0')),   where k = min(i, K).

The intuition behind this recurrence relation is that we can either perform an operation on the last K characters of S[0:i] (if i >= K) or on all the characters in S[0:i] except for the last character (if i < K). If we perform an operation on the last K characters, then we can reduce the number of zeroes in those characters by 1, and the remaining number of zeroes in S[0:i] is simply dp[i-k][j-1]. Otherwise, if we do not perform an operation on the last K characters, then the remaining number of zeroes in S[0:i] is simply dp[i-1][j] plus 1 if the i-th character is ‘0’.

The final answer is dp[N][Q].

Algorithm:

  1.    Define a function ‘minZeroes‘ that takes the binary string S, a 2D vector ‘dp‘, integers N, K and Q as input parameters.
  2.    Check for the base cases:
           a. If k or q is 0, then return the count of 0’s in the string.
           b. If q is greater than or equal to the maximum possible number of operations, return 0.
           c. If the result for current parameters is already calculated, return it from the memoization table.
  3.    If the result is not present in the memoization table, then use the recurrence relation given below to calculate the minimum number of            remaining zeroes:
                  dp[n][q] = min(minZeroes(s, dp, n – 1, k, q) + (s[n – 1] == ‘0’), minZeroes(s, dp, max(n – k, 0), k, q – 1)).
  4.    Store the calculated result in the memoization table.
  5.    Return the calculated result.
  6.    In the main function:
           a. Define the input parameters S, N, K, and Q.
           b. Create a 2D vector ‘dp‘ of size (N+1)x(Q+1), initialized with -1.
           c. Call the ‘minZeroes‘ function with the input parameters S, dp, N, K, and Q.
           d. Print the result.

Below is the implementation of the above approach: 

C++




// C++ code for the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the minimum number of remaining
// zeroes in the binary string
int minZeroes(string s, vector<vector<int> >& dp, int n,
              int k, int q)
{
    // Base cases
    if (k == 0 || q == 0) {
        return count(
            // Count the number of 0s in the string
            s.begin(), s.end(), '0');
    }
 
    // If q is greater than or equal to the
    // maximum possible number of operations,
    // then no 0s will remain
    if (q >= (n + k - 1) / k) {
        return 0;
    }
 
    // If the result for current
    // parameters is already
    // calculated, return it from the
    // memoization table
    if (dp[n][q] != -1) {
        return dp[n][q];
    }
 
    // Recurrence relation to calculate the minimum number
    // of remaining zeroes
    dp[n][q] = min(
        minZeroes(s, dp, n - 1, k, q) + (s[n - 1] == '0'),
        minZeroes(s, dp, max(n - k, 0), k, q - 1));
 
    // Return the calculated result
    return dp[n][q];
}
 
// Driver's code
int main()
{
    string S = "0100010110";
    int N = S.size();
    int K = 3, Q = 2;
 
    // 2D vector to store the dp states,
    // initially having all the values
    // as INT_MAX
    vector<vector<int> > dp(N + 1, vector<int>(Q + 1, -1));
 
    // Call the function to calculate
    // the minimum number of remaining
    // zeroes and print the result
    cout << minZeroes(S, dp, N, K, Q);
    return 0;
}


Java




import java.util.Arrays;
 
public class Main {
    // Function to calculate the minimum number of remaining
    // zeroes in the binary string
    static int minZeroes(String s, int[][] dp, int n, int k, int q) {
        // Base cases
        if (k == 0 || q == 0) {
            return (int) s.chars().filter(ch -> ch == '0').count();
        }
 
        // If q is greater than or equal to the
        // maximum possible number of operations,
        // then no 0s will remain
        if (q >= (n + k - 1) / k) {
            return 0;
        }
 
        // If the result for current
        // parameters is already
        // calculated, return it from the
        // memoization table
        if (dp[n][q] != -1) {
            return dp[n][q];
        }
 
        // Recurrence relation to calculate the minimum number
        // of remaining zeroes
        dp[n][q] = Math.min(
                minZeroes(s, dp, n - 1, k, q) + (s.charAt(n - 1) == '0' ? 1 : 0),
                minZeroes(s, dp, Math.max(n - k, 0), k, q - 1)
        );
 
        // Return the calculated result
        return dp[n][q];
    }
 
    // Driver's code
    public static void main(String[] args) {
        String S = "0100010110";
        int N = S.length();
        int K = 3, Q = 2;
 
        // 2D array to store the dp states,
        // initially having all the values
        // as -1
        int[][] dp = new int[N + 1][Q + 1];
        for (int[] row : dp) {
            Arrays.fill(row, -1);
        }
 
        // Call the function to calculate
        // the minimum number of remaining
        // zeroes and print the result
        System.out.println(minZeroes(S, dp, N, K, Q));
    }
}


Python




def min_zeroes(s, dp, n, k, q):
    # Base cases
    if k == 0 or q == 0:
        return s.count('0')
 
    # If q is greater than or equal to the
    # maximum possible number of operations,
    # then no 0s will remain
    if q >= (n + k - 1) // k:
        return 0
 
    # If the result for current
    # parameters is already
    # calculated, return it from the
    # memoization table
    if dp[n][q] != -1:
        return dp[n][q]
 
    # Recurrence relation to calculate the minimum number
    # of remaining zeroes
    dp[n][q] = min(
        min_zeroes(s, dp, n - 1, k, q) + (1 if s[n - 1] == '0' else 0),
        min_zeroes(s, dp, max(n - k, 0), k, q - 1)
    )
 
    # Return the calculated result
    return dp[n][q]
 
# Driver's code
if __name__ == "__main__":
    S = "0100010110"
    N = len(S)
    K, Q = 3, 2
 
    # 2D array to store the dp states,
    # initially having all the values
    # as -1
    dp = [[-1] * (Q + 1) for _ in range(N + 1)]
 
    # Call the function to calculate
    # the minimum number of remaining
    # zeroes and print the result
    print(min_zeroes(S, dp, N, K, Q))


C#




using System;
 
public class MinZeroes {
    public static int MinZeroesCount(string s, int n, int k,
                                     int q)
    {
        int[, ] dp = new int[n + 1, q + 1];
 
        // Base case 1: If k or q is 0, return the count of
        // '0's in the string.
        if (k == 0 || q == 0) {
            return s.Split('0').Length - 1;
        }
 
        // Base case 2: If q is greater than or equal to the
        // maximum possible number of operations, return 0.
        if (q >= (n + k - 1) / k) {
            return 0;
        }
 
        // Initialize dp[i][0] for all i.
        for (int i = 1; i <= n; i++) {
            dp[i, 0]
                = dp[i - 1, 0] + (s[i - 1] == '0' ? 1 : 0);
        }
 
        // Fill in the dp table using the recurrence
        // relation.
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= q; j++) {
                int minZeroes = int.MaxValue;
 
                for (int x = 1; x <= Math.Min(i, k); x++) {
                    minZeroes = Math.Min(minZeroes,
                                         dp[i - x, j - 1]);
                }
 
                dp[i, j] = Math.Min(
                    minZeroes,
                    dp[i - 1, j]
                        + (s[i - 1] == '0' ? 1 : 0));
            }
        }
 
        return dp[n, q];
    }
 
    public static void Main(string[] args)
    {
        string S = "0100010110";
        int N = S.Length;
        int K = 3;
        int Q = 2;
 
        int result = MinZeroesCount(S, N, K, Q);
 
        Console.WriteLine(
            "Minimum number of remaining zeroes: "
            + result);
    }
}


Javascript




// Function to calculate the minimum number of remaining
// zeroes in the binary string
function minZeroes(s, dp, n, k, q) {
    // Base cases
    if (k === 0 || q === 0) {
        return s.split('0').length - 1;
    }
 
    // If q is greater than or equal to the
    // maximum possible number of operations,
    // then no 0s will remain
    if (q >= Math.ceil(n / k)) {
        return 0;
    }
 
    // If the result for current parameters is already
    // calculated, return it from the memoization table
    if (dp[n][q] !== -1) {
        return dp[n][q];
    }
 
    // Recurrence relation to calculate the minimum number
    // of remaining zeroes
    dp[n][q] = Math.min(
        minZeroes(s, dp, n - 1, k, q) + (s[n - 1] === '0' ? 1 : 0),
        minZeroes(s, dp, Math.max(n - k, 0), k, q - 1)
    );
 
    // Return the calculated result
    return dp[n][q];
}
 
// Driver's code
const S = "0100010110";
const N = S.length;
const K = 3;
const Q = 2;
 
// 2D array to store the dp states,
// initially having all the values
// as Infinity
const dp = new Array(N + 1);
for (let i = 0; i <= N; i++) {
    dp[i] = new Array(Q + 1).fill(-1);
}
 
// Call the function to calculate
// the minimum number of remaining
// zeroes and print the result
console.log(minZeroes(S, dp, N, K, Q));
 
// This code is contributed by akshitaguprzj3


Output

2






Time Complexity: O(N*Q) where N is size of input string and Q is number of operations. This is because for each i and j, the function dp(i, j) makes at most two recursive calls and each call takes constant time. Therefore, the number of unique function calls made by the algorithm is bounded by N*Q, and each call takes constant time. Hence, the overall time complexity is O(N*Q).

Auxiliary Space: O(N * Q), which is used to store the values of the subproblems in the 2D DP table. Here N is size of input string and Q is number of operations.

Approach 2: Using Dynamic Programming

Steps:

  1. Create a 2D array dp[][], where dp[i][j] represents the minimum number of zeroes in the prefix of length i of the given string after j operations. Below are the steps to follow:
  2. Initially, for all values of j in range [0, Q], dp[0][j] = 0 and for all values of i in range [0, N], dp[i][0] = count of 0 in prefix of length i.
  3. For each stated dp[i][j], there are two possible choices of operations:
    • Perform an operation on the substring S[i – K, i]. In such cases, dp[i][j] = dp[i – K][j – 1].
    • Do not perform any operation. In such cases, dp[i][j] = dp[i – 1][j] + X, where the value of X is 1 if the value of S[i – 1] = 0, Otherwise, X = 0.
  4. Therefore, the DP relation of the above problem can be stated as:

dp[i][j] = min(dp[i – k][j – 1], dp[i – 1][j] + (S[i – 1] == ‘0’)), for all i in the range [1, N] and for all j in the range [1, Q].

5. After completing the steps, the value stored at dp[N][Q] is the required answer.

Below is the implementation of the above approach: 

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum number of
// zeroes after changing the value of all
// elements of any substring of at most K
// elements to 1 at most Q number of times
int minZeroeCount(string S, int K, int Q)
{
 
    // Stores the size of the given string
    int N = S.size();
 
    // If no operation is possible, return
    // the count of 0 in the initial string
    if (K == 0 || Q == 0) {
        return count(S.begin(), S.end(), '0');
    }
 
    // If all the elements of the given string
    // can be selected in Q operations
    if (Q >= ceil((float)N / K)) {
        return 0;
    }
 
    // 2D vector to store the dp states,
    // initially having all the values
    // as INT_MAX
    vector<vector<int> > dp(N + 1,
                            vector<int>(Q + 1, INT_MAX));
 
    // Loop to initialize dp[0][j] = 0
    // for all j in the range [0, Q],
    for (int j = 0; j <= Q; ++j) {
        dp[0][j] = 0;
    }
 
    // Loop to initialize dp[0][j] = count
    // of 0 in prefix of length i for all
    // i in the range [1, N],
    for (int i = 1; i <= N; ++i) {
        dp[i][0] = dp[i - 1][0] + (S[i - 1] == '0');
    }
 
    // Traverse over all dp[][] states
    for (int i = 1; i <= N; ++i) {
        for (int j = 1; j <= Q; ++j) {
 
            // Find the value of dp[i][j]
            dp[i][j]
                = min(dp[max(0, i - K)][j - 1],
                      dp[i - 1][j] + (S[i - 1] == '0'));
        }
    }
 
    // Return answer
    return dp[N][Q];
}
 
// Driver Code
int main()
{
    string S = "0100010110";
    int K = 3;
    int Q = 2;
 
    cout << minZeroeCount(S, K, Q);
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
 
class GFG
{
   
      // Function to find the minimum number of
    // zeroes after changing the value of all
    // elements of any substring of at most K
    // elements to 1 at most Q number of times
    static int minZeroeCount(String S, int K, int Q)
    {
 
        // Stores the size of the given string
        int N = S.length();
 
        // If no operation is possible, return
        // the count of 0 in the initial string
        if (K == 0 || Q == 0)
        {
              int c = 0;
            for(int i = 0; i < S.length(); i++) {
                  if(S.charAt(i) == '0')
                  c++;
            }
              return c;
        }
 
        // If all the elements of the given string
        // can be selected in Q operations
        if (Q >= Math.ceil((float)N / K)) {
            return 0;
        }
 
        // 2D vector to store the dp states,
        // initially having all the values
        // as INT_MAX
        int[][] dp = new int[N + 1][Q + 1];
 
        // Loop to initialize dp[0][j] = 0
        // for all j in the range [0, Q],
        for (int j = 0; j <= Q; ++j) {
            dp[0][j] = 0;
        }
 
        // Loop to initialize dp[0][j] = count
        // of 0 in prefix of length i for all
        // i in the range [1, N],
        for (int i = 1; i <= N; ++i) {
            if (S.charAt(i - 1) == '0')
                dp[i][0] = dp[i - 1][0] + 1;
            else
                dp[i][0] = dp[i - 1][0];
        }
 
        // Traverse over all dp[][] states
        for (int i = 1; i <= N; ++i) {
            for (int j = 1; j <= Q; ++j) {
 
                // Find the value of dp[i][j]
 
                if (S.charAt(i - 1) == '0')
                    dp[i][j] = Math.min(
                        dp[(Math.max(0, i - K))][j - 1],
                        dp[i - 1][j] + 1);
                else
                    dp[i][j] = Math.min(
                        dp[(Math.max(0, i - K))][j - 1],
                        dp[i - 1][j]);
            }
        }
 
        // Return answer
        return dp[N][Q];
    }
 
    // Driver Code
    public static void main (String[] args) {
        String S = "0100010110";
        int K = 3;
        int Q = 2;
 
        System.out.println(minZeroeCount(S, K, Q));
    }
}
 
// This code is contributed by Dharanendra L V.


Python3




# Python 3 program for the above approach
import sys
def cnt(s,ch):
    count  = 0
    for x in s:
        if x == ch:
            count += 1
    return count
 
# Function to find the minimum number of
# zeroes after changing the value of all
# elements of any substring of at most K
# elements to 1 at most Q number of times
def minZeroeCount(S, K, Q):
   
    # Stores the size of the given string
    N = len(S)
 
    # If no operation is possible, return
    # the count of 0 in the initial string
    if (K == 0 or Q == 0):
        return cnt(S,'0')
 
    # If all the elements of the given string
    # can be selected in Q operations
    if (Q >= int(N/K) + 1):
        return 0
 
    # 2D vector to store the dp states,
    # initially having all the values
    # as INT_MAX
    dp = [[sys.maxsize for i in range(Q + 1)] for j in range(N + 1)]
 
    # Loop to initialize dp[0][j] = 0
    # for all j in the range [0, Q],
    for j in range(Q + 1):
        dp[0][j] = 0
 
    # Loop to initialize dp[0][j] = count
    # of 0 in prefix of length i for all
    # i in the range [1, N],
    for i in range(1,N+1,1):
        dp[i][0] = dp[i - 1][0] + (S[i - 1] == '0')
 
    # Traverse over all dp[][] states
    for i in range(1, N + 1, 1):
        for j in range(1, Q + 1, 1):
           
            # Find the value of dp[i][j]
            dp[i][j] = min(dp[max(0, i - K)][j - 1], dp[i - 1][j]+(S[i - 1] == '0'))
 
    # Return answer
    return dp[N][Q]
 
# Driver Code
if __name__ == '__main__':
    S = "0100010110"
    K = 3
    Q = 2
    print(minZeroeCount(S, K, Q))
     
    # This code is contributed by SURENDRA_GANGWAR.


C#




// C# program for the above approach
using System;
using System.Linq;
class GFG {
    // Function to find the minimum number of
    // zeroes after changing the value of all
    // elements of any substring of at most K
    // elements to 1 at most Q number of times
    static int minZeroeCount(string S, int K, int Q)
    {
 
        // Stores the size of the given string
        int N = S.Length;
 
        // If no operation is possible, return
        // the count of 0 in the initial string
        if (K == 0 || Q == 0)
        {
            return S.Count(c => c == '0');
        }
 
        // If all the elements of the given string
        // can be selected in Q operations
        if (Q >= Math.Ceiling((float)N / K)) {
            return 0;
        }
 
        // 2D vector to store the dp states,
        // initially having all the values
        // as INT_MAX
        int[, ] dp = new int[N + 1, Q + 1];
 
        // Loop to initialize dp[0][j] = 0
        // for all j in the range [0, Q],
        for (int j = 0; j <= Q; ++j) {
            dp[0, j] = 0;
        }
 
        // Loop to initialize dp[0][j] = count
        // of 0 in prefix of length i for all
        // i in the range [1, N],
        for (int i = 1; i <= N; ++i) {
            if (S[i - 1] == '0')
                dp[i, 0] = dp[i - 1, 0] + 1;
            else
                dp[i, 0] = dp[i - 1, 0];
        }
 
        // Traverse over all dp[][] states
        for (int i = 1; i <= N; ++i) {
            for (int j = 1; j <= Q; ++j) {
 
                // Find the value of dp[i][j]
 
                if (S[i - 1] == '0')
                    dp[i, j] = Math.Min(
                        dp[(Math.Max(0, i - K)), j - 1],
                        dp[i - 1, j] + 1);
                else
                    dp[i, j] = Math.Min(
                        dp[(Math.Max(0, i - K)), j - 1],
                        dp[i - 1, j]);
            }
        }
 
        // Return answer
        return dp[N, Q];
    }
 
    // Driver Code
    public static void Main()
    {
        string S = "0100010110";
        int K = 3;
        int Q = 2;
 
        Console.WriteLine(minZeroeCount(S, K, Q));
    }
}
 
// This code is contributed by ukasp.


Javascript




<script>
 
    // JavaScript program for the above approach
 
    const INT_MAX = 2147483647;
    // Function to find the minimum number of
    // zeroes after changing the value of all
    // elements of any substring of at most K
    // elements to 1 at most Q number of times
    const minZeroeCount = (S, K, Q) => {
 
        // Stores the size of the given string
        let N = S.length;
 
        // If no operation is possible, return
        // the count of 0 in the initial string
        if (K == 0 || Q == 0) {
            let cnt0 = 0;
            for (let i = 0; i < N; ++i) {
                if (S[i] == '0') cnt0++;
            }
            return cnt0;
        }
 
        // If all the elements of the given string
        // can be selected in Q operations
        if (Q >= Math.ceil(N / K)) {
            return 0;
        }
 
        // 2D vector to store the dp states,
        // initially having all the values
        // as INT_MAX
        const dp = new Array(N + 1).fill(INT_MAX).map(() => new Array(Q + 1).fill(INT_MAX));
 
        // Loop to initialize dp[0][j] = 0
        // for all j in the range [0, Q],
        for (let j = 0; j <= Q; ++j) {
            dp[0][j] = 0;
        }
 
        // Loop to initialize dp[0][j] = count
        // of 0 in prefix of length i for all
        // i in the range [1, N],
        for (let i = 1; i <= N; ++i) {
            dp[i][0] = dp[i - 1][0] + (S[i - 1] == '0');
        }
 
        // Traverse over all dp[][] states
        for (let i = 1; i <= N; ++i) {
            for (let j = 1; j <= Q; ++j) {
 
                // Find the value of dp[i][j]
                dp[i][j]
                    = Math.min(dp[(Math.max(0, i - K))][j - 1], dp[i - 1][j] + (S[i - 1] == '0'));
            }
        }
 
        // Return answer
        return dp[N][Q];
    }
 
    // Driver Code
    let S = "0100010110";
    let K = 3;
    let Q = 2;
 
    document.write(minZeroeCount(S, K, Q));
 
    // This code is contributed by rakeshsahni
 
</script>


 
 

Output

2






 Time Complexity: O(N*Q)
Auxiliary Space: O(N*Q)



Similar Reads

Minimize hamming distance in Binary String by setting only one K size substring bits
Given two binary strings S and T of length N and a positive integer K. Initially, all characters of T are '0'. The task is to find the minimum Hamming distance after choosing a substring of size K and making all elements of string T as '1' only once. Examples: Input: S = "101", K = 2Output: 1Explanation: Initially string T = "000", one possible way
7 min read
Minimize flips to make binary string as all 1s by flipping characters in substring of size K repeatedly
Given a binary string S of size N and an integer K, the task is to find the minimum number of operations required to make all characters as 1s in the binary string by flipping characters in the substring of size K. If it is not possible to do so, then print "-1". Examples: Input: S = "00010110 ", K = 3Output: 3Explanation:Below are the operations p
7 min read
Find if a given string can be represented from a substring by iterating the substring ā€œnā€ times
Given a string 'str', check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. Examples: Input: str = "abcabcabc" Output: true The given string is 3 times repetition of "abc" Input: str = "abadabad" Output: true The given string is 2 times repetition of "abad" Input: str = "aabaabaabaab" Ou
20 min read
Minimize the number by changing at most K digits
Given a number N, the task is to minimize the number by changing at most K digits. Note that the number should not contain any leading zeros.Examples: Input: N = 91945, K = 3 Output: 10045Input: N = 1, K = 0 Output: 1 Approach: Replace the first digit with 1 if its not already 1 and update K accordingly.Now for the rest of the digits, replace the n
6 min read
Minimize replacement of bits to make the count of 01 substring equal to 10 substring
Given a binary string str. The task is to minimize the number of replacements of '0' by '1' or '1' by '0' to balance the binary string. A binary string is said to be balanced: "if the number of "01" substring = number of "10" substring". Examples: Input: str = "101010" Output: 1Explanation: "01" = 2 &amp; "10" = 3. So change the last character to '
4 min read
Minimize operations to make all array elements -1 by changing maximums of K-size subarray to -1
Given an array arr[] consisting of N integers and an integer K, the task is to find the minimum of operations required to make all the array elements -1 such that in each operation, choose a subarray of size K and change all the maximum element in the subarray to -1. Examples: Input: arr[] = {18, 11, 18, 11, 18}, K = 3 Output: 3Explanation:Followin
8 min read
Find Binary String of size at most 3N containing at least 2 given strings of size 2N as subsequences
Given three binary strings a, b, and c each having 2*N characters each, the task is to find a string having almost 3*N characters such that at least two of the given three strings occur as its one of the subsequence. Examples: Input: a = "00", b = "11", c = "01"Output: "010"Explanation: The strings "00" and "01" are subsequences of string "010" and
11 min read
Sum of all numbers formed having 4 atmost X times, 5 atmost Y times and 6 atmost Z times
Given three integers X, Y and Z, the task is to find the sum of all the numbers formed having 4 at most X times, 5 at most Y times, and 6 at most Z times, under mod 10^9+7.Examples: Input: X = 1, Y = 1, Z = 1 Output: 3675 Explanation: 4 + 5 + 6 + 45 + 54 + 56 + 65 + 46 + 64 + 456 + 465 + 546 + 564 + 645 + 654 = 3675 Input: X = 4, Y = 5, Z = 6 Outpu
9 min read
Count of binary strings of length N having equal count of 0's and 1's and count of 1's &ge; count of 0's in each prefix substring
Given an integer N, the task is to find the number of possible binary strings of length N with an equal frequency of 0's and 1's in which frequency of 1's are greater or equal to the frequency of 0's in every prefix substring. Examples: Input: N = 2Output: 1Explanation:All possible binary strings of length 2 are {"00", "01", "10" and "11"}. Out of
7 min read
Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
Given binary string str, the task is to count the number of substrings of the given string str such that each character of the substring belongs to a palindromic substring of length at least 2. Examples: Input: S = "00111" Output: 6 Explanation: There are 6 such substrings in the given string such that each character belongs to a palindrome of size
6 min read