检查是否可以通过递归删除子字符串“abc”使仅由 a、b、c 组成的字符串为空
给定一个大小为N的字符串S ,仅由字符' a '、' b ' 和 ' c ' 组成,任务是通过递归删除字符串“abc”来检查给定字符串是否可以为空。如果发现是真的,则打印“是” 。否则,打印“否” 。
例子:
Input: S = abcabc
Output: Yes
Explanation:
Below are the operations performed to empty the string:
- Remove the substring S[3, 5] from the string modifies the string S to “abc”.
- Remove the substring S[0, 2] from the string modifies the string S to “”.
After the above operations, the given string S can be made empty by removing substring “abc”. Therefore, print Yes.
Input: S = abcabcababccc
Output: No
方法:给定的问题可以通过使用堆栈来解决。这个想法是通过删除所有出现的“abc”来将字符串最小化为空字符串。请按照以下步骤解决给定的问题:
- 初始化一个堆栈,比如Stack来存储给定字符串S的字符。
- 遍历给定的字符串S并执行以下步骤:
- 如果当前字符是“ a ”或“ b ”,则将其压入堆栈Stack 。
- 如果当前字符是' c ',最后两个字符分别是' b '和' a ',则从堆栈中弹出两次。
- 如果当前字符是' c '并且最后两个字符不是' b '和' a ',那么给定的字符串S就不能形成。
- 完成上述步骤后,如果堆栈为空,则打印“是” 。否则,打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if the given
// string S can be made empty
string canMadeEmpty(string s, int n)
{
// Stores the characters
// of the string S
stack St;
// Traverse the given string
for (int i = 0; i < n; i++) {
// If the character is c
if (s[i] == 'c') {
// If stack size is greater
// than 2
if (St.size() >= 2) {
// Pop from the stack
char b = St.top();
St.pop();
char a = St.top();
St.pop();
// Top two characters in
// the stack should be 'b'
// and 'a' respectively
if (a != 'a' || b != 'b')
return "No";
}
// Otherwise, print No
else
return "No";
}
// If character is 'a' or 'b'
// push to stack
else
St.push(s[i]);
}
// If stack is empty, then print
// Yes. Otherwise print No
if (St.size() == 0) {
return "Yes";
}
else {
return "No";
}
}
// Driver Code
int main()
{
string S = "aabcbc";
int N = S.length();
cout << canMadeEmpty(S, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to check if the given
// String S can be made empty
static String canMadeEmpty(String s, int n)
{
// Stores the characters
// of the String S
Stack St = new Stack();
// Traverse the given String
for (int i = 0; i < n; i++)
{
// If the character is c
if (s.charAt(i) == 'c') {
// If stack size is greater
// than 2
if (St.size() >= 2) {
// Pop from the stack
char b = St.peek();
St.pop();
char a = St.peek();
St.pop();
// Top two characters in
// the stack should be 'b'
// and 'a' respectively
if (a != 'a' || b != 'b')
return "No";
}
// Otherwise, print No
else
return "No";
}
// If character is 'a' or 'b'
// push to stack
else
St.add(s.charAt(i));
}
// If stack is empty, then print
// Yes. Otherwise print No
if (St.size() == 0) {
return "Yes";
}
else {
return "No";
}
}
// Driver Code
public static void main(String[] args)
{
String S = "aabcbc";
int N = S.length();
System.out.print(canMadeEmpty(S, N));
}
}
// This code is contributed by Princi Singh.
Python3
# Python program for the above approach
# Function to check if the given
# string S can be made empty
def canMadeEmpty(s, n):
# Stores the characters
# of the string S
st = []
# Traverse the given string
for i in range(n):
# If the character is c
if s[i] == 'c':
# If stack size is greater
# than 2
if len(st) >= 2:
# Pop from the stack
b = st[-1]
st.pop()
a = st[-1]
st.pop()
# Top two characters in
# the stack should be 'b'
# and 'a' respectively
if a != 'a' or b != 'b':
return "No"
# Otherwise, print No
else:
return "No"
# If character is 'a' or 'b'
# push to stack
else:
st.append(s[i])
# If stack is empty, then print
# Yes. Otherwise print No
if len(st) == 0:
return "Yes"
else:
return "No"
# Driver code
s = "aabcbc"
n = len(s)
print(canMadeEmpty(s, n))
# This code is contributed by Parth Manchanda
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to check if the given
// String S can be made empty
static String canMadeEmpty(String s, int n)
{
// Stores the characters
// of the String S
Stack St = new Stack();
// Traverse the given String
for (int i = 0; i < n; i++)
{
// If the character is c
if (s[i] == 'c') {
// If stack size is greater
// than 2
if (St.Count >= 2) {
// Pop from the stack
char b = St.Peek();
St.Pop();
char a = St.Peek();
St.Pop();
// Top two characters in
// the stack should be 'b'
// and 'a' respectively
if (a != 'a' || b != 'b')
return "No";
}
// Otherwise, print No
else
return "No";
}
// If character is 'a' or 'b'
// push to stack
else
St.Push(s[i]);
}
// If stack is empty, then print
// Yes. Otherwise print No
if (St.Count == 0) {
return "Yes";
}
else {
return "No";
}
}
// Driver Code
public static void Main(String[] args)
{
String S = "aabcbc";
int N = S.Length;
Console.Write(canMadeEmpty(S, N));
}
}
// This code is contributed by shikhasingrajput
Javascript
输出:
Yes
时间复杂度: O(N)
辅助空间: O(N)