我想尝试创建一个算法来删除字符串中的重复字符串。
例如
输入:Hello 输出:Helo
输入:AAAAZZZZ5 输出:AZ5
输入:“苹果、苹果和橙子” 输出:“苹果和橙子”
我写了下面的算法(JSFiddle here)
function removeRepeat(str)
{
var index = 0;
var tempS = str.length;
var currentBuffer = "";
var repeatCharIndex = 1;
console.log(str);
for (var i = 1; i < tempS; i++)
{
var curChar = str[i];
for (var j = 0; j < i; j++)
{
// check if duplicate
if (str[j] === curChar)
{
console.log("duplicate detected at index ",j,str[j],"and index",i,str[i])
// we have duplicate! means we could potentially have a repeated set of characters
// i, j have same character, so let's move both forward
var aheadLeft=j, aheadRight=i;
var diff = Math.min(aheadRight-aheadLeft,tempS-aheadRight);
var repeat = true;
for (var num = 1; num < diff; num++)
{
// we go backwards...
// ashiash ...
// we are at __h___h, so now we go
// _s__s_
console.log("\tis ",str[aheadRight+num],str[aheadLeft+num])
if (str[aheadRight+num] !== str[aheadLeft+num])
{
repeat = false;
break;
}
}
if (repeat){
console.log("found repeat!",str,str[aheadLeft],aheadLeft,str[aheadRight],aheadRight);
str = str.substring(0,aheadRight)+str.substring(aheadRight+diff)
return removeRepeat(str);
}
break;
}
}
}
return str;
}
console.log("New str: "+removeRepeat("nnnnnnnnzzzzzz1"));
我遇到的问题是算法没有产生正确的结果"Apples and Apples and Oranges"
重复的字符串应该是Apples and
,结果应该是 Apples and Oranges 但我得到了
Aples and Apples and Orang
我不确定如何修复我的算法以检查重复项是否是更大图景的一部分。我的一个想法是向后而不是向前穿过绳子。任何想法/提示都会很棒!
*编辑:我在原始示例中不够清楚。
输入Hey Hi Hi Hi Hey Hi Hi Hi
应该输出,Hey Hi Hi Hi
而不是Hey Hi
因为Hi Hi Hi
,虽然重复,是较大的一部分Hey Hi Hi Hi
Boots and Cats and Boots and Cats and YO
应该等于Boots and Cats Yo
不Bots and Cats and Boots and Cats and YO