X1689: Remove adjacent duplicate letters

Description

Write a recursive Java method that removes adjacent duplicate characters from a String. If two identical characters appear next to each other, keep only one of them in the result.

A simple way to do this recursively is to check the first two characters of a string and if they are the same, keep only 1 and repeat (recursively) for the rest of the string.

Examples

  • removeAdjDuplicate("bookkeeper") → "bokeper"
  • removeAdjDuplicate("Mississippi") → "Misisipi"
  • removeAdjDuplicate("aabbcc") → "abc"

Constraints

  • Must write a recursive method, do not use loops.
  • The method should return the cleaned String.

Reference

You may consult the Java documentation for these classes:

Examples:

removeAdjDuplicate("bookkeeper") -> "bokeper"
removeAdjDuplicate("Mississippi") -> "Misisipi"

Your Answer:

Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.