给定一个大小为N的二进制字符串S ,任务是检查是否可以将字符串划分为等于“010”的不相交子序列。
例子:
Input: S = “010100”
Output: Yes
Explanation: Partitioning the string in the manner 010100 to generate two subsequences equal to “010”.
Input: S = “010000”
Output: No
方法:这个想法是基于这样的观察:如果以下任何一个条件成立,给定的二进制字符串将不满足所需的条件:
- 如果在任何时候, “1”的前缀计数大于“0”的前缀计数。
- 如果在任何时候, “1”的后缀计数大于“0”的后缀计数。
- 如果‘0’的计数不等于整个字符串中‘1’的计数的两倍。
请按照以下步骤解决问题:
- 初始化一个布尔变量, res为真以检查字符串S 是否满足给定条件。
- 创建两个变量count0和count1来存储字符串S中 0 和 1 的频率。
- 使用变量i遍历[0, N – 1]范围内的字符串S
- 如果S[i]等于1 ,则将count1的值增加1 。
- 否则,将count0的值增加1 。
- 检查count1 > count0的值,然后将res更新为false 。
- 检查count0的值是否不等于2 * count1 ,然后将res更新为false 。
- 将count0和count1的值重置为0 。
- 以相反的方向遍历字符串S并重复步骤3.1到3.3 。
- 如果res的值仍然为true ,则打印“Yes”作为结果,否则打印“No” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if the given string
// can be partitioned into a number of
// subsequences all of which are equal to "010"
bool isPossible(string s)
{
// Store the size
// of the string
int n = s.size();
// Store the count of 0s and 1s
int count_0 = 0, count_1 = 0;
// Traverse the given string
// in the forward direction
for (int i = 0; i < n; i++) {
// If the character is '0',
// increment count_0 by 1
if (s[i] == '0')
++count_0;
// If the character is '1'
// increment count_1 by 1
else
++count_1;
// If at any point,
// count_1 > count_0,
// return false
if (count_1 > count_0)
return false;
}
// If count_0 is not equal
// to twice count_1,
// return false
if (count_0 != (2 * count_1))
return false;
// Reset the value of count_0 and count_1
count_0 = 0, count_1 = 0;
// Traverse the string in
// the reverse direction
for (int i = n - 1; i >= 0; --i) {
// If the character is '0'
// increment count_0
if (s[i] == '0')
++count_0;
// If the character is '1'
// increment count_1
else
++count_1;
// If count_1 > count_0,
// return false
if (count_1 > count_0)
return false;
}
return true;
}
// Driver Code
int main()
{
// Given string
string s = "010100";
// Function Call
if (isPossible(s))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program for the above approach
public class MyClass
{
// Function to check if the given string
// can be partitioned into a number of
// subsequences all of which are equal to "010"
static boolean isPossible(String s)
{
// Store the size
// of the string
int n = s.length();
// Store the count of 0s and 1s
int count_0 = 0, count_1 = 0;
// Traverse the given string
// in the forward direction
for (int i = 0; i < n; i++) {
// If the character is '0',
// increment count_0 by 1
if (s.charAt(i) == '0')
++count_0;
// If the character is '1'
// increment count_1 by 1
else
++count_1;
// If at any point,
// count_1 > count_0,
// return false
if (count_1 > count_0)
return false;
}
// If count_0 is not equal
// to twice count_1,
// return false
if (count_0 != (2 * count_1))
return false;
// Reset the value of count_0 and count_1
count_0 = 0; count_1 = 0;
// Traverse the string in
// the reverse direction
for (int i = n - 1; i >= 0; --i) {
// If the character is '0'
// increment count_0
if (s.charAt(i) == '0')
++count_0;
// If the character is '1'
// increment count_1
else
++count_1;
// If count_1 > count_0,
// return false
if (count_1 > count_0)
return false;
}
return true;
}
// Driver Code
public static void main(String args[])
{
// Given string
String s = "010100";
// Function Call
if (isPossible(s))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by SoumikMondal
Python3
# Python3 program for the above approach
# Function to check if the given string
# can be partitioned into a number of
# subsequences all of which are equal to "010"
def isPossible(s):
# Store the size
# of the string
n = len(s)
# Store the count of 0s and 1s
count_0, count_1 = 0, 0
# Traverse the given string
# in the forward direction
for i in range(n):
# If the character is '0',
# increment count_0 by 1
if (s[i] == '0'):
count_0 += 1
# If the character is '1'
# increment count_1 by 1
else:
count_1 += 1
# If at any point,
# count_1 > count_0,
# return false
if (count_1 > count_0):
return False
# If count_0 is not equal
# to twice count_1,
# return false
if (count_0 != (2 * count_1)):
return False
# Reset the value of count_0 and count_1
count_0, count_1 = 0, 0
# Traverse the string in
# the reverse direction
for i in range(n - 1, -1, -1):
# If the character is '0'
# increment count_0
if (s[i] == '0'):
count_0 += 1
# If the character is '1'
# increment count_1
else:
count_1 += 1
# If count_1 > count_0,
# return false
if (count_1 > count_0):
return False
return True
# Driver Code
if __name__ == '__main__':
# Given string
s = "010100"
# Function Call
if (isPossible(s)):
print("Yes")
else:
print("No")
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if the given string
// can be partitioned into a number of
// subsequences all of which are equal to "010"
static bool isPossible(String s)
{
// Store the size
// of the string
int n = s.Length;
// Store the count of 0s and 1s
int count_0 = 0, count_1 = 0;
// Traverse the given string
// in the forward direction
for(int i = 0; i < n; i++)
{
// If the character is '0',
// increment count_0 by 1
if (s[i] == '0')
++count_0;
// If the character is '1'
// increment count_1 by 1
else
++count_1;
// If at any point,
// count_1 > count_0,
// return false
if (count_1 > count_0)
return false;
}
// If count_0 is not equal
// to twice count_1,
// return false
if (count_0 != (2 * count_1))
return false;
// Reset the value of count_0 and count_1
count_0 = 0;
count_1 = 0;
// Traverse the string in
// the reverse direction
for(int i = n - 1; i >= 0; --i)
{
// If the character is '0'
// increment count_0
if (s[i] == '0')
++count_0;
// If the character is '1'
// increment count_1
else
++count_1;
// If count_1 > count_0,
// return false
if (count_1 > count_0)
return false;
}
return true;
}
// Driver code
static public void Main()
{
// Given string
String s = "010100";
// Function Call
if (isPossible(s))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by offbeat
Javascript
输出:
Yes
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。