3

I'm trying to remove duplicate characters from a string recursively. I don't know how to fix this code to remain the first character when characters have different cases.

/**
     * Remove consecutive duplicate characters from a String. <br>
     * Case should not matter, if two or more consecutive duplicate <br>
     * characters have different cases, then the first letter should be kept.
     * @param word A word with possible consecutive duplicate characters.
     * @return A word without any consecutive duplicate characters.
     */

    public static String dedupeChars(String word){

        if ( word.length() <= 1 )
            return word;

       if( word.substring(0,1).equalsIgnoreCase(word.substring(1,2)) )       
            return dedupeChars(word.substring(1));

       else
            return word.substring(0,1) + dedupeChars(word.substring(1));

    }
1
  • "I don't know how to fix this code" what you trying to fix? Commented Sep 18, 2020 at 5:50

2 Answers 2

4

You were on the right track, but your logic was a bit off. Consider this version, with explanation below the code:

public static String dedupeChars(String word) {
    if (word.length() <= 1) {
        return word;
    }

    if (word.substring(0,1).equalsIgnoreCase(word.substring(1,2))) {
        return dedupeChars(word.substring(0, 1) + word.substring(2));
    }
    else {
        return word.substring(0,1) + dedupeChars(word.substring(1));
    }
}

System.out.println(dedupeChars("aaaaBbBBBbCDdefghiIIiJ"));

This prints:

aBCDefghiJ

For an explanation of the algorithm, your base case was correct, and for a single character word, we just return than character. For the case where the first character be identical to the second one, we splice out that second character and then recursively call dedupeChars() again. For example, here is what happens with the input string shown above:

aaaaBbBBBbCDdefghiIIiJ
aaaBbBBBbCDdefghiIIiJ
aaBbBBBbCDdefghiIIiJ
aBbBBBbCDdefghiIIiJ

That is, we splice out duplicates, always retaining the first occurrence, until there are no more duplicates.

By the way, in practice you might instead want to use regex here, for a much more concise solution:

String input = "aaaaBbBBBbCDdefghiIIiJ";
input = input.replaceAll("(?i)(.)\\1+", "$1");
System.out.println(input);

This prints:

aBCDefghiJ

Here we just tell the regex engine to remove all duplicates of any single letter, retaining only the first letter in the series.

Sign up to request clarification or add additional context in comments.

Comments

0

I have a different way to achieve your purpose and I think your code is too expensive to remove duplicate characters(ignore uppercase or lowercase,just keep the first one).

public static String removeDup(String s) {
char[] chars = s.toCharArray();
StringBuilder sb = new StringBuilder();
for (int i = chars.length - 1; i > 0; i--) {
  if (chars[i] == chars[i - 1]) {
    continue;
  }
  if (chars[i] < 97) {
    if (chars[i] == (chars[i - 1] - 32)) {
      continue;
    }
  } else {
    if (chars[i] == (chars[i - 1] + 32)) {
      continue;
    }
  }
  sb.append(chars[i]);
}
sb.append(chars[0]);
return sb.reverse().toString();}

For the input "aaaaBbBBBbCDdefghiIIiJ" the output will be "aBCDefghiJ"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.