Question

Given a string, recursively compress all sets of repeating adjacent chars within an existing string to a single char. For example, XVxzzz yields xyz <pre> ceoveReaeatslfffaaac Quuutreturns far Out eoveReneatsCnogoge wogorcriiies) returns no worries remaveReneats.C Tomorrow) returns Iomeo s /pre Qparam stc a string of characters @return a version of the original string with all repeating adjacent sequences of the same character, reduced to a single character public static String removeRepeats (String str) { ou are forbidden to use any loops, nor String or List based methods such as contains, or methods that use regular expressions

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class RemoveRepeats {

    public static String removeRepeats(String str) {
        if(str.length() <= 1) {
            return str;
        } else {
            if(str.charAt(0) == str.charAt(1)) {
                return "" + removeRepeats(str.substring(1));
            } else {
                return str.charAt(0) + removeRepeats(str.substring(1));
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(removeRepeats("fffaaar Ouuut"));
        System.out.println(removeRepeats("nooooo wooorriiiies"));
        System.out.println(removeRepeats("Tomorrow"));

    }

}

far Out no worie3 Tomorow Process finished with e xit code 0

Add a comment
Know the answer?
Add Answer to:
Given a string, recursively compress all sets of repeating adjacent chars within an existing string to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all...

    Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all others intact and follow similar guidelines. The methods that need to be changed are in the code below. - Rewrite the indexOf() method. Remove the existing recursive implementation of the method, and replace it with one that uses iteration instead. - Rewrite the isPrefix() method so that it uses iteration. Remove the existing recursive implementation of the method, and replace it with one that...

  • JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using...

    JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using recursion. The method signatures are included in the starter code below, with a more detailed explanation of what function the method should perform. You will be writing the following methods: stringClean() palindromeChecker() reverseString() totalWord() permutation() You will be using tools in the String class like .substring(), .charAt(), and .length() in all of these methods, so be careful with indices. If you get stuck, think...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT