通过递归删除 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
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短