From ef745e07f72cfc37fc56500fd3cc7d8d52fdd71e Mon Sep 17 00:00:00 2001 From: Maxouuuuu <56126588+Maxouuuuu@users.noreply.github.com> Date: Fri, 8 Nov 2019 21:39:11 +0100 Subject: [PATCH] Create dedup.java BJP5 Exercise 12.14: dedup --- chapter-12/dedup.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 chapter-12/dedup.java diff --git a/chapter-12/dedup.java b/chapter-12/dedup.java new file mode 100644 index 0000000..a0b1dfa --- /dev/null +++ b/chapter-12/dedup.java @@ -0,0 +1,15 @@ +/* Write a recursive method called dedup that takes a string as a parameter and that returns a new string +obtained by replacing every sequence of repeated adjacent letters with just one of that letter. For example, +the string "bookkkkkeeper" has three repeated adjacent letters ("oo", "kkkkk", and "ee"), so the call of dedup("bookkkkkeeper") +should return "bokeper". Do not call the string replace or split methods in your solution.*/ + +public static String dedup(String s) { + if (s.length() == 1) { + return s; + } + if (s.charAt(0) != s.charAt(1)) { + String newS = Character.toString(s.charAt(0)); + return newS + dedup(s.substring(1)); + } + return dedup(s.substring(1)); + }