通过递归删除给定的子字符串,检查字符串是否可以变为空

给定一个字符串“str”和另一个字符串“sub_str”。我们可以多次从“str”中删除“sub_str”。还指出“sub_str”一次只出现一次。任务是通过反复删除“sub_str”来确定“str”是否可以变为空。 例如:

null
Input  : str = "GEEGEEKSKS", sub_str = "GEEKS"Output : YesExplanation : In the string GEEGEEKSKS, we can first               delete the substring GEEKS from position 4.              The new string now becomes GEEKS. We can               again delete sub-string GEEKS from position 1.               Now the string becomes empty.Input  : str = "GEEGEEKSSGEK", sub_str = "GEEKS"Output : NoExplanation : In the string it is not possible to make the              string empty in any possible manner.

解决这个问题的一个简单方法是使用内置 一串 函数find()和erase()。首先在原始字符串str中输入用于搜索目的的子字符串substr,然后使用find()迭代原始字符串以查找子字符串的索引,如果未找到,则返回原始字符串中子字符串的起始索引else-1,并使用erase()擦除该子字符串,直到原始字符串的长度大于0。 上述简单的解决方案之所以有效,是因为给定的子字符串一次只出现一次。

C++

// C++ Program to check if a string can be
// converted to an empty string by deleting
// given sub-string from any position, any
// number of times.
#include<bits/stdc++.h>
using namespace std;
// Returns true if str can be made empty by
// recursively removing sub_str.
bool canBecomeEmpty(string str, string sub_str)
{
while (str.size() > 0)
{
// idx: to store starting index of sub-
//      string found in the original string
int idx = str.find(sub_str);
if (idx == -1)
break ;
// Erasing the found sub-string from
// the original string
str.erase(idx, sub_str.size());
}
return (str.size() == 0);
}
// Driver code
int main()
{
string str = "GEEGEEKSKS" , sub_str = "GEEKS" ;
if (canBecomeEmpty(str, sub_str))
cout<< "Yes" ;
else
cout<< "No" ;
return 0;
}


JAVA

//Java program to check if a string can be
// converted to an empty string by deleting
// given sub-string from any position, any
// number of times.
class GFG {
// Returns true if str can be made empty by
// recursively removing sub_str.
static boolean canBecomeEmpty(String str, String sub_str) {
while (str.length() > 0 ) {
// idx: to store starting index of sub-
//      string found in the original string
int idx = str.indexOf(sub_str);
if (idx == - 1 ) {
break ;
}
// Erasing the found sub-string from
// the original string
str = str.replaceFirst(sub_str, "" );
}
return (str.length() == 0 );
}
// Driver code
public static void main(String[] args) {
String str = "GEEGEEKSKS" , sub_str = "GEEKS" ;
if (canBecomeEmpty(str, sub_str)) {
System.out.print( "Yes" );
} else {
System.out.print( "No" );
}
}
}
// This code is contributed by 29AjayKumar


Python3

# Python3 program to check if a string can be
# converted to an empty string by deleting
# given sub-string from any position, any
# number of times.
# Returns true if str can be made empty by
# recursively removing sub_str.
def canBecomeEmpty(string, sub_str):
while len (string) > 0 :
# idx: to store starting index of sub-
#     string found in the original string
idx = string.find(sub_str)
if idx = = - 1 :
break
# Erasing the found sub-string from
# the original string
string = string.replace(sub_str, "", 1 )
return ( len (string) = = 0 )
# Driver code
if __name__ = = "__main__" :
string = "GEEGEEKSKS"
sub_str = "GEEKS"
if canBecomeEmpty(string, sub_str):
print ( "Yes" )
else :
print ( "No" )
# This code is contributed by
# sanjeev2552


C#

// C# program to check if a string can be
// converted to an empty string by deleting
// given sub-string from any position, any
// number of times.
using System;
class GFG
{
// Returns true if str can be made empty by
// recursively removing sub_str.
static Boolean canBecomeEmpty(String str, String sub_str)
{
while (str.Length > 0)
{
// idx: to store starting index of sub-
//     string found in the original string
int idx = str.IndexOf(sub_str);
if (idx == -1)
{
break ;
}
// Erasing the found sub-string from
// the original string
str = str.Replace(sub_str, "" );
}
return (str.Length == 0);
}
// Driver code
public static void Main(String[] args)
{
String str = "GEEGEEKSKS" , sub_str = "GEEKS" ;
if (canBecomeEmpty(str, sub_str))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
// This code is contributed by 29AjayKumar


Javascript

<script>
// Javascript program to check if a string can be
// converted to an empty string by deleting
// given sub-string from any position, any
// number of times.
// Returns true if str can be made empty by
// recursively removing sub_str
function canBecomeEmpty(str,sub_str)
{
while (str.length > 0)
{
// idx: to store starting index of sub-
//      string found in the original string
let idx = str.indexOf(sub_str);
if (idx == -1) {
break ;
}
// Erasing the found sub-string from
// the original string
str = str.replace(sub_str, "" );
}
return (str.length == 0);
}
// Driver code
let str = "GEEGEEKSKS" , sub_str = "GEEKS" ;
if (canBecomeEmpty(str, sub_str)) {
document.write( "<br>Yes" );
} else {
document.write( "<br>No" );
}
// This code is contributed by rag2127
</script>


输出:

Yes

本文由 希曼舒古普塔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享