a*****a 发帖数: 46 | 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) | j******7 发帖数: 15 | 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)
|
|