How can I remove consecutive duplicates recursively without an int parameter?

I need to fix my previous code. If the input is "aabbcdefgghijkllaa", the output should be "abcdefghijkla". My current code, which I changed, produces "alkjihgfedcba". (The input may not have to be in alphabetical order, and the output should be in the same order as the original input.) Do I just have to create a new method to cancel it, or is there a way to fix it using my current methods?

Here is my code:

package recursion;

public class recursion {

    static String a = "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 to 0 (and from - to +) gives the result only "a", which is also not what I need. How can i fix this?

+4
source share
4

:

  • , null
  • < 2
  • a >= 2

... .

, ; , . , .


, , "aabccdde". , . -, .

:

private static String deleteConsecutiveDuplicates(String string,
                                                  char lastSeenLetter,
                                                  StringBuilder builder)

, -, , : " /null?" .

static String deleteConsecutiveDuplicates(String string) {

    if(null == string || string.length() == 0) {
        return string;
    } else {
        return deleteConsecutiveDuplicates(string.substring(1),
                                           string.charAt(0),
                                           new StringBuilder());
    }
}

, , .

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

, , .

  • , [2, n), " " 1 .

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

.

  • b
  • , [1, n), " " 0 .

, "abcde".


, , . , - .

, ( , , .. "zzzzzzz" "z" ), , - .

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();
}
+2

?

return a.replaceAll("(.)(?=\\1)", "");

, .. abbbc -> abc. , .. abbbc -> abbc, :

return a.replaceAll("(.)\\1", "$1");
0

- , ( ):

public class recursion {

    static String a = "aabbcdefgghijkllaa";

    public static void main(String[] args){

        System.out.println("Input: " + a);
        System.out.println("Output: " + removeDuplicates(a));
    }

    public static String removeDuplicates(String a){
        if(a.length() < 2)
            return a;
        return a.charAt(0)+removeDuplicates(a.substring(a.charAt(0) == a.charAt(1) ? 2 : 1));
    }
}

removeDuplicates , , , , , .

0
source

Are you sure you want to use recursion for this? If you really want to remove duplicate characters, you can do this with Set.

Set s = new HashSet();
String a = "aabbcdefgghijkllaa";
for (int i=0 ;i<a.length();i++) {
    s.add(a.charAt(i));
}
System.out.println(s);
-one
source

All Articles