Check if a string consisting only of a, b, c can be made empty by removing substring “abc” recursively
Last Updated :
10 Aug, 2021
Given a string S size of N consisting of characters ‘a‘, ‘b‘, and ‘c‘ only, the task is to check if the given string can be made empty by removing the string “abc” recursively or not. If found to be true, then print “Yes”. Otherwise, print “No”.
Examples:
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
Approach: The given problem can be solved by using a stack. The idea is to minimize the string to an empty string by removing all occurrences of the “abc”. Follow the steps below to solve the given problem:
- Initialize a stack, say Stack to store the characters of the given string S.
- Traverse the given string S and perform the following steps:
- If the current character is ‘a‘ or ‘b‘, then push it to the stack Stack.
- If the current character is ‘c‘ and the last two characters are ‘b‘ and ‘a‘ respectively, then pop twice from the stack.
- If the current character is ‘c‘ and the last two characters are not ‘b‘ and ‘a‘, then the given string S can not be formed.
- After completing the above steps, if the stack is empty, then print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string canMadeEmpty(string s, int n)
{
stack< char > St;
for ( int i = 0; i < n; i++) {
if (s[i] == 'c' ) {
if (St.size() >= 2) {
char b = St.top();
St.pop();
char a = St.top();
St.pop();
if (a != 'a' || b != 'b' )
return "No" ;
}
else
return "No" ;
}
else
St.push(s[i]);
}
if (St.size() == 0) {
return "Yes" ;
}
else {
return "No" ;
}
}
int main()
{
string S = "aabcbc" ;
int N = S.length();
cout << canMadeEmpty(S, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static String canMadeEmpty(String s, int n)
{
Stack<Character> St = new Stack<Character>();
for ( int i = 0 ; i < n; i++)
{
if (s.charAt(i) == 'c' ) {
if (St.size() >= 2 ) {
char b = St.peek();
St.pop();
char a = St.peek();
St.pop();
if (a != 'a' || b != 'b' )
return "No" ;
}
else
return "No" ;
}
else
St.add(s.charAt(i));
}
if (St.size() == 0 ) {
return "Yes" ;
}
else {
return "No" ;
}
}
public static void main(String[] args)
{
String S = "aabcbc" ;
int N = S.length();
System.out.print(canMadeEmpty(S, N));
}
}
|
Python3
def canMadeEmpty(s, n):
st = []
for i in range (n):
if s[i] = = 'c' :
if len (st) > = 2 :
b = st[ - 1 ]
st.pop()
a = st[ - 1 ]
st.pop()
if a ! = 'a' or b ! = 'b' :
return "No"
else :
return "No"
else :
st.append(s[i])
if len (st) = = 0 :
return "Yes"
else :
return "No"
s = "aabcbc"
n = len (s)
print (canMadeEmpty(s, n))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static String canMadeEmpty(String s, int n)
{
Stack< char > St = new Stack< char >();
for ( int i = 0; i < n; i++)
{
if (s[i] == 'c' ) {
if (St.Count >= 2) {
char b = St.Peek();
St.Pop();
char a = St.Peek();
St.Pop();
if (a != 'a' || b != 'b' )
return "No" ;
}
else
return "No" ;
}
else
St.Push(s[i]);
}
if (St.Count == 0) {
return "Yes" ;
}
else {
return "No" ;
}
}
public static void Main(String[] args)
{
String S = "aabcbc" ;
int N = S.Length;
Console.Write(canMadeEmpty(S, N));
}
}
|
Javascript
<script>
function canMadeEmpty(s, n) {
let St = [];
for (let i = 0; i < n; i++) {
if (s[i] == 'c' ) {
if (St.length >= 2) {
let b = St[St.length - 1];
St.pop();
let a = St[St.length - 1];
St.pop();
if (a != 'a' || b != 'b' )
return "No" ;
}
else
return "No" ;
}
else
St.push(s[i]);
}
if (St.length == 0) {
return "Yes" ;
}
else {
return "No" ;
}
}
let S = "aabcbc" ;
let N = S.length;
document.write(canMadeEmpty(S, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...