- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a string can become empty by recursively deleting a given sub-string in C++
Suppose, we are given two strings, str1 and str2. str2 is a substring of str1, and we can delete str2 from str1. It is possible, that the string str2 appears multiple times in str1. Our goal here is to find out if str1 becomes a null string if we keep removing str2 from str1 multiple times. If it is possible we return 1, otherwise 0.
So, if the input is like str1 = "CCCPPPPPP", str2 = "CPP"; then the output will be true.
To solve this, we will follow these steps −
- while size of str1 > 0, do −
- index := return the string start position of str2 in str1
- if index is same as -1, then −
- Come out from the loop
- delete str2 from str1
- return 1 if size of str1 is similar to 0, otherwise 0.
Example (C++)
Let us see the following implementation to get better understanding −
#include<bits/stdc++.h> using namespace std; bool solve(string str1, string str2) { while (str1.size() > 0) { int index = str1.find(str2); if (index == -1) break; str1.erase(index, str2.size()); } return (str1.size() == 0); } int main() { string str1 = "CCCPPPPPP", str2 = "CPP"; cout<<solve(str1, str2)<<endl; return 0; }
Input
"CCCPPPPPP", "CPP"
Output
1
- Related Articles
- Check if a string can become empty by recursively deleting a given sub-string in Python
- String slicing in Python to check if a can become empty by recursive deletion
- Check if a string contains a sub-string in C++
- Check if a string contains a palindromic sub-string of even length in C++
- How to check if a string contains a specific sub string?
- Check if a given string is sum-string in C++
- Check If a String Can Break Another String in C++
- Method to check if a String contains a sub string ignoring case in Java
- Check if a string contains a palindromic sub-string of even length in Python
- Meta Strings (Check if two strings can become same after a swap in one string) in C++
- Check if given string can be formed by concatenating string elements of list in Python
- Check if a String is empty ("") or null in Java
- How to check if a string is empty in Kotlin?
- Write a program in C++ to check if a string can be obtained by rotating another string by two places
- Check if a string can be formed from another string using given constraints in Python

Advertisements