📌  相关文章
📜  计算对二进制字符串进行分区的方法,以便每个子字符串恰好包含两个 0

📅  最后修改于: 2021-10-26 05:48:51             🧑  作者: Mango

给定二进制字符串str ,任务是找到对字符串进行分区的方法的计数,使得每个分区的子字符串恰好包含两个0

例子:

方法:想法是计算给定字符串的每两个连续0之间的1计数。请按照以下步骤解决问题:

  • 初始化一个数组,比如IdxOf0s[] ,以在给定的字符串存储0的索引。
  • 迭代指定字符串的字符和0的索引存储到IdxOf0s []。
  • 初始化一个变量,比如cntWays ,以存储对字符串进行分区的方法数,以便每个分区都包含两个0
  • 如果给定字符串中0的计数为奇数,则更新cntWays = 0
  • 否则,遍历数组IdxOf0s[]并使用cntWays *= (IdxOf0s[i] – IdxOf0s[i – 1])计算对每个分区恰好为两个0的数组进行分区的方法数
  • 最后,打印cntWays的值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find count of ways to partition
// the string such that each partition
// contains exactly two 0s.
int totalWays(int n, string str)
{
 
    // Stores indices of 0s in
    // the given string.
    vector IdxOf0s;
 
    // Store the count of ways to partition
    // the string such that each partition
    // contains exactly two 0s.
    int cntWays = 1;
 
    // Iterate over each characters
    // of the given string
    for (int i = 0; i < n; i++) {
 
        // If current character is '0'
        if (str[i] == '0') {
 
            // Insert index
            IdxOf0s.push_back(i);
        }
    }
 
    // Stores total count of 0s in str
    int M = IdxOf0s.size();
 
    if (M == 0 or M % 2) {
 
        return 0;
    }
 
    // Traverse the array, IdxOf0s[]
    for (int i = 2; i < M; i += 2) {
 
        // Update cntWays
        cntWays = cntWays * (IdxOf0s[i]
                             - IdxOf0s[i - 1]);
    }
 
    return cntWays;
}
 
// Driver Code
int main()
{
    string str = "00100";
 
    int n = str.length();
 
    cout << totalWays(n, str);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
       
// Function to find count of ways to partition
// the string such that each partition
// contains exactly two 0s.
static int totalWays(int n, String str)
{
   
    // Stores indices of 0s in
    // the given string.
    ArrayList IdxOf0s = 
                    new ArrayList();
   
    // Store the count of ways to partition
    // the string such that each partition
    // contains exactly two 0s.
    int cntWays = 1;
   
    // Iterate over each characters
    // of the given string
    for (int i = 0; i < n; i++)
    {
   
        // If current character is '0'
        if (str.charAt(i) == '0')
        {
   
            // Insert index
            IdxOf0s.add(i);
        }
    }
   
    // Stores total count of 0s in str
    int M = IdxOf0s.size();
    if ((M == 0) || ((M % 2) != 0))
    {
        return 0;
    }
   
    // Traverse the array, IdxOf0s[]
    for (int i = 2; i < M; i += 2)
    {
   
        // Update cntWays
        cntWays = cntWays * (IdxOf0s.get(i)
                             - IdxOf0s.get(i - 1));
    } 
    return cntWays;
}
   
// Driver code
public static void main(String[] args)
{
    String str = "00100";
    int n = str.length();
    System.out.print(totalWays(n, str));
}
}
 
// This code is contributed by sanjoy_62.


Python3
# Python3 program for the above approach
 
# Function to find count of ways to partition
# thesuch that each partition
# contains exactly two 0s.
def totalWays(n, str):
     
    # Stores indices of 0s in
    # the given string.
    IdxOf0s = []
 
    # Store the count of ways to partition
    # the such that each partition
    # contains exactly two 0s.
    cntWays = 1
 
    # Iterate over each characters
    # of the given string
    for i in range(n):
         
        # If current character is '0'
        if (str[i] == '0'):
             
            # Insert index
            IdxOf0s.append(i)
 
    # Stores total count of 0s in str
    M = len(IdxOf0s)
 
    if (M == 0 or M % 2):
        return 0
 
    # Traverse the array, IdxOf0s[]
    for i in range(2, M, 2):
         
        # Update cntWays
        cntWays = cntWays * (IdxOf0s[i] -
                             IdxOf0s[i - 1])
 
    return cntWays
 
# Driver Code
if __name__ == '__main__':
     
   str = "00100"
   n = len(str)
    
   print(totalWays(n, str))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections; 
using System.Collections.Generic; 
 
class GFG
{
 
  // Function to find count of ways to partition
  // the string such that each partition
  // contains exactly two 0s.
  static int totalWays(int n, string str)
  {
 
    // Stores indices of 0s in
    // the given string.
    ArrayList IdxOf0s
      = new ArrayList();
 
    // Store the count of ways to partition
    // the string such that each partition
    // contains exactly two 0s.
    int cntWays = 1;
 
    // Iterate over each characters
    // of the given string
    for (int i = 0; i < n; i++)
    {
 
      // If current character is '0'
      if (str[i] == '0')
      {
 
        // Insert index
        IdxOf0s.Add(i);
      }
    }
 
    // Stores total count of 0s in str
    int M = IdxOf0s.Count;
    if ((M == 0) || ((M % 2) != 0)) {
      return 0;
    }
 
    // Traverse the array, IdxOf0s[]
    for (int i = 2; i < M; i += 2)
    {
 
      // Update cntWays
      cntWays = cntWays * (Convert.ToInt32(IdxOf0s[i]) -
                           Convert.ToInt32(IdxOf0s[i - 1]));
    }
    return cntWays;
  }
 
  // Driver code
  static public void Main()
  {
    string str = "00100";
    int n = str.Length;
    Console.Write(totalWays(n, str));
  }
}
 
// This code is contributed by Dharanendra L V


Javascript


输出:
2

时间复杂度: O(N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程