avatar
这个怎么做?# JobHunting - 待字闺中
a*a
1
一道题:You are given a string S and a set of n substrings. You are supposed
to remove every instance of those n substrings from S so that S is of the
minimum length and output this minimum length. Eg: S- ccdaabcdbb n=2 -
substrings– (ab, cd)Output: 2 Explanation: ccdaabcdbb -> ccdacdbb -> cabb -
> cb (length=2)
avatar
j*7
2
刚学python,菜鸟一个,见笑了。
str="Heeololeo"
sub=["ol","ee"]
for i in range(len(sub)-1):
for j in range(len(str)-1):
if str[j:j+len(sub[i])]==sub[i]:
str=str.replace(sub[i],"")
return str
上面的循环只能去掉ol,无法执行第二次大循环。
而且感觉还应该在最外面加一个循环,break条件是 sub[i]不再是最近一次结果的
substring。

supposed
-

【在 a*****a 的大作中提到】
: 一道题:You are given a string S and a set of n substrings. You are supposed
: to remove every instance of those n substrings from S so that S is of the
: minimum length and output this minimum length. Eg: S- ccdaabcdbb n=2 -
: substrings– (ab, cd)Output: 2 Explanation: ccdaabcdbb -> ccdacdbb -> cabb -
: > cb (length=2)

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。