Check if a string consisting only of a, b, c can be made empty by removing substring "abc" recursively (original) (raw)

Last Updated : 23 Jul, 2025

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:

  1. Remove the substring S[3, 5] from the string modifies the string S to "abc".
  2. 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:

Below is the implementation of the above approach:

C++ `

// C++ program for the above approach

#include <bits/stdc++.h> 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<Character> St = new Stack<Character>();

// 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<char> St = new Stack<char>();

// 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

`

Time Complexity: O(N)
Auxiliary Space: O(N)