java - How can I delete consecutive duplicates recursively without an int parameter? -


i need fix previous code. if input "aabbcdefgghijkllaa", output needs "abcdefghijkla". current code have changed outputs "alkjihgfedcba". (the input may not in alphabetical order, , output should in same order original input.) should create new method reverse or there way fix current methods?

here code:

package recursion;  public class recursion {      static string = "aabbcdefgghijkllaa";     static int b=a.length()-1;     static string result = "";      public static void main(string[] args){          system.out.println("input: " + a);         system.out.print("output: ");         removeduplicates(a);     }      public static string removeduplicates(string a){          if (b <= 0){             system.out.print(a.charat(0));             result=result+a.charat(0);         }         else if (a.charat(b) == a.charat(b-1)) {             b--;             removeduplicates(a);                       }         else {             system.out.print(a.charat(b));             result=result+a.charat(b);             b--;             removeduplicates(a);         }         return a;     } } 

changing b 0 (and - +) gives output of "a", not need. how can fix this?

it may better break specific goals instead:

  • what want when string null or empty
  • what want string of size < 2
  • what want string of size >= 2

...and conditions each scenario.

note code follows untested; it's meant give rough idea not complete solution. works examples, definitely has shortcomings it.


let's think happens when example string "aabccdde". want keep track of first letter in string rest of string. wouldn't hurt have append either.

with that, come signature recursive method:

private static string deleteconsecutiveduplicates(string string,                                                   char lastseenletter,                                                   stringbuilder builder) 

this isn't expose anyone, cover method initial, "are empty/null?" checks.

static string deleteconsecutiveduplicates(string string) {      if(null == string || string.length() == 0) {         return string;     } else {         return deleteconsecutiveduplicates(string.substring(1),                                            string.charat(0),                                            new stringbuilder());     } } 

let's talk when see string.

round 1: 'a' | "abccdde" | ""

the character last saw same first character in string.

  • add builder
  • advance substring denoted index [2, n), , take "last seen letter" character @ position 1 of passed in string.

round 2: 'b' | "ccdde" | "a"

the 2 characters different.

  • append b
  • advance substring denoted index [1, n) , take "last seen letter" character @ position 0 of passed in string.

repeat these steps until left string "abcde".


the ideology here break problem smaller sub-problems easier digest , tackle. in general, you're ever adding 1 character @ time through last seen letter variable.

there other edge cases simple approach doesn't account (one letter, multiple repeated instances of it; i.e. "zzzzzzz" leave off one "z"), hope give somewhere start.

private static string deleteconsecutiveduplicates(string string, char lastseenletter, stringbuilder builder) {     if(string.length() == 0) {         builder.append(lastseenletter);     } else if(string.charat(0) == lastseenletter) {         builder.append(lastseenletter);         if(string.length() < 2 ) {             return builder.tostring();         }         return deleteconsecutiveduplicates(string.substring(2), string.charat(1), builder);     } else {         if(builder.charat(builder.length() - 1) != lastseenletter) {             builder.append(lastseenletter);         }         return deleteconsecutiveduplicates(string.substring(1), string.charat(0), builder);     }     return builder.tostring(); } 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -