给定一个二进制字符串str ,任务是找到对该字符串进行分区的方法数,以使每个分区子字符串恰好包含两个0 。
例子:
Input: str = “00100”
Output:
Explanation:
Possible ways to partition the string such that each partition contains exactly two 0s are: { {“00”, “100”}, {“001”, “00”} }.
Therefore, the required output is 2.
Input: str = “000”
Output: 0
方法:想法是计算给定字符串的每两个连续0 s之间的1 s计数。请按照以下步骤解决问题:
- 初始化一个数组,例如IdxOf0s [] ,以将0的索引存储在给定的字符串。
- 迭代指定字符串的字符和0的索引存储到IdxOf0s []。
- 初始化一个变量,例如cntWays ,以存储对字符串进行分区的方式的计数,以使每个分区恰好包含两个0 。
- 如果给定字符串的0 s计数为奇数,则更新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
输出:
2
时间复杂度: O(N)
辅助空间: O(N)