Tuesday, 15 January 2013

algorithm - Remove Repeated Strings from String -


I wanted to try to create an algorithm to remove the string repeatedly in the string.

For example Input: Hello output: Hello

Input: AAAZZZ5 Output: AJ5

Input: "Apple and Apple and oranges "Output:" Sepels and oranges "

I wrote the algorithm below ()

  Delete function repeat (str) {var index = 0; Var tempS = str.length; Var currentBuffer = ""; Var repeatCharIndex = 1; Console.log (STR); (Var i = 1; i & lt; tempS; i ++) for {var curChar = str [i]; For (var j = 0; j & lt; i; j ++) {// check if duplicate (str [j] === curChar] {console.log ("duplicate detected in index", which, St [j] "and index", i, str [i]) // We have a duplicate! It means that we possibly can have a duplicated set of repeating characters / I, J is the same character, then let's Forward forward further for further movement, forward Left = J, front right = var diff = Math.min (forwardRight-forwardLeft, tempS-forwardRight); var repeat = true; (Var num = 1; num & lt; Diff; num ++) {// We go backwards ... // a Shiash ... // we are at __h___h, so now we go to // _s__s_ console.log ("\ tis", str [forwardRight + num], str [forward lift + num]) if (str [forwardRight + num]! == str [forwardLeft + num]) {repeat = false; breakdown;}} if (repeat) {console.log ("repeat found!", Str, str [ahead], forward lift, str [forwardRight], forward WR; str = str.substring (0, forwardRight) + str.substring (forwardRight + diff) Return withdrawal (str);} break;}}} return string;} Console.log ("New Straw:" + Delete repeat ("Nnnnnnnnzzzzzz1"));   

The problem is that the algorithm does not produce the correct result for "apples and apples and oranges"

repeated string Apple and and the result should be apples and oranges but I should have

  ales and sepals and oranges   

I do not know this Whether a duplicate is part of a bigger picture or not, to see how to fix my algorithm. I had an idea that the strings were to move forward rather than backwards.

input hi hi hello hi hello hi hello

* edit: My original example was not clear enough. Code> should output hi hi hi instead of hey because hi hi hi , while repeating, big hi hello

Boots and cats and shoes and cats and yo should be same boots and cats yo not bots and cats and shoes and cats And Yo

The suggestion I give is a function the longest Duplicate Exit Time And then call it many times if you think so. This is the easiest way to get rid of (mostly) ambiguity in my imagination.

And if you want to do this, you get the code, but instead of actually deleting the code, just track how much will be removed, and where every time you want to delete more , Then update that information.

Then, finally, remove the largest found part (the information you have kept).

No comments:

Post a Comment