给定一个二进制字符串str ,任务是检查字符串可以通过删除形式为“10”的所有子序列来清空
例子:
Input: str = “11011000”
Output: Yes
Explanation:Following steps are required to be performed to empty the given string “11011000” → “111000” → “1100” → “10” → “”
Input: 101001
Output: No
处理方法:按照以下步骤解决问题:
- 遍历字符串并将所有1存储在堆栈中。
- 如果str[i] = 1 ,则将其存储在堆栈中。
- 如果str[i] = 0且堆栈不为空,则弹出堆栈顶部的字符。
- 否则,返回false 。
- 如果堆栈为空,则返回true 。
- 否则返回false
下面是上述方法的实现:
C++
// C++ program for
// the above approach
#include
using namespace std;
// Function to find if string
// is reducible to NULL
bool isReducible(string str)
{
// Length of string
int N = str.size();
// Stack to store all 1s
stack s;
// Iterate over the characters
// of the string
for (int i = 0; i < N; i++) {
// If current character is 1
if (str[i] == '1')
// Push it into the stack
s.push(str[i]);
else if (!s.empty())
// Pop from the stack
s.pop();
else
// If the stack is empty
return false;
}
return s.empty();
}
// Driver Code
int main()
{
string str = "11011000";
if (isReducible(str))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program for
// the above approach
import java.io.*;
import java.util.*;
class GFG
{
// Function to find if string
// is reducible to NULL
static boolean isReducible(String str)
{
// Length of string
int N = str.length();
// Stack to store all 1s
ArrayList s = new ArrayList();
// Iterate over the characters
// of the string
for (int i = 0; i < N; i++)
{
// If current character is 1
if (str.charAt(i) == '1')
// Push it into the stack
s.add(str.charAt(i));
else if (s.size() > 0)
// Pop from the stack
s.remove(s.size() - 1);
else
// If the stack is empty
return false;
}
if(s.size() == 0)
{
return true;
}
else{
return false;
}
}
// Driver code
public static void main (String[] args)
{
String str = "11011000";
if (isReducible(str))
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
}
// This code is contributed by avanitrachhadiya2155
Python3
# Python3 program for the above approach
# Function to find if string
# is reducible to NULL
def isReducible(Str) :
# Length of string
N = len(Str)
# Stack to store all 1s
s = []
# Iterate over the characters
# of the string
for i in range(N) :
# If current character is 1
if (Str[i] == '1') :
# Push it into the stack
s.append(Str[i])
elif(len(s) > 0) :
# Pop from the stack
del s[len(s) - 1]
else :
# If the stack is empty
return False
if(len(s) == 0) :
return True
else :
return False
# Driver code
Str = "11011000"
if (isReducible(Str)) :
print("Yes")
else :
print("No")
# This code is contributed by divyeshrabadiya07.
C#
// C# program for
// the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to find if string
// is reducible to NULL
static bool isReducible(string str)
{
// Length of string
int N = str.Length;
// Stack to store all 1s
List s = new List();
// Iterate over the characters
// of the string
for (int i = 0; i < N; i++) {
// If current character is 1
if (str[i] == '1')
// Push it into the stack
s.Add(str[i]);
else if (s.Count > 0)
// Pop from the stack
s.RemoveAt(s.Count - 1);
else
// If the stack is empty
return false;
}
if(s.Count == 0)
{
return true;
}
else{
return false;
}
}
// Driver code
static void Main()
{
string str = "11011000";
if (isReducible(str))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by divyesh072019.
Javascript
输出:
Yes
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live