小编hao*_*mao的帖子

是否有快速算法来删除字符串中重复的子串?

有一个像它的字符串

dxabcabcyyyydxycxcxz
Run Code Online (Sandbox Code Playgroud)

我想将它合并到

dxabcydxycxz
Run Code Online (Sandbox Code Playgroud)

其他例子: ddxddx - > dxdx,abbab - > abab.

规则是:

if (adjacent and same): merge

# Such as 'abc',they are same and , so I will delete one of them .
# Although 'dx' is same as 'dx',they are nonadjacent,so I do not delete any of them
# If one character has been deleted, we don't delete any sub-string include it 
Run Code Online (Sandbox Code Playgroud)

我在python的代码中完成了它,但是当它在一个长字符串中时它很慢.

# original string
mystr = "dxabcabcyyyydxycxcxz"
str_len = len(mystr)
vis = [1] *str_len #Use a …
Run Code Online (Sandbox Code Playgroud)

python string algorithm

9
推荐指数
2
解决办法
1098
查看次数

标签 统计

algorithm ×1

python ×1

string ×1