描述
给定一个只包含小写字母的字符串,现在某字符串如ha被众字符串围困在其中,需要我们将字符串中的 ha 全部移除然后后输出,保证最后输出的字符串中没有 "ha"。
输入
一行数据包含一个字符串,长度 <= 100000,字符串仅包含小写字母。
输出
输出处理后的字符串
输入样例
hawihavvo
chufighhhaaaoppo
samhaunhaple
输出
hawivvo
chufigoppo
samhaunple
分析:
这里其实主要是一个字符串替换的问题,最基本的思路是,一次遍历字符串,每次取当前字符及后面的一个字符,判断如果是指定待替换的字符串,则替换为空,并将字符串长度缩减,用c或c++语言实现思路简单,但写起来代码不应该很短,可以考虑使用python。废话不多说,上代码。
def replaceString(line, target): response=line.strip().replace(target,"") while target in response: response=response.strip().replace(target,"") return response if __name__ == "__main__": line="chufighhhaaaoppo" dstStr="ha" print replaceString(line,dstStr)