通过递归删除 C++ 中的给定子字符串来检查字符串是否可以变空
假设我们有两个字符串,str1和str2。str2是str1的子串,我们可以从str1中删除str2。字符串str2有可能在str1中出现多次。我们的目标是找出如果我们多次从str1中删除str2,则str1是否变为空字符串。如果可能,我们返回1,否则返回0。
所以,如果输入像str1="CCCPPPPPPP",str2="CPP";那么输出将为真。
示例(C++)
让我们看看以下实现以获得更好的理解-
#includeusing 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< 输入
"CCCPPPPPP", "CPP"输出结果1
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下