Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8924141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:19:49+00:00 2026-06-15T07:19:49+00:00

I am stuck on this CodingBat recursion problem: Given a string, return recursively a

  • 0

I am stuck on this CodingBat recursion problem:

Given a string, return recursively a “cleaned” string where adjacent chars that are the same have been reduced to a single char. So “yyzzza” yields “yza”.

stringClean("yyzzza") → "yza"
stringClean("abbbcdd") → "abcd"
stringClean("Hello") → "Helo"

I can solve it using loops, but this is not allowed since the problem is supposed so be solved using recursion. Is there any way to solve this problem without using a loop and using only recursion? No global variables, no loops. I even thought of encoding some information in the parameter but that would be cheating too I think.

My previous program had no while loop, and I could only get half of the answers right. Basically, when I called my function with the string parameter, I checked the first 2 characters. If they were the same, I would return the character and call the function again with a string two characters smaller. A string of 3 or 4 of the same consecutive characters would always defeat my algorithm however.

public String stringClean(String str) {

    if (str.length() == 0)
        return "";

    if (str.length() > 1) {

    int counter = 1;


      char a = str.charAt(0);
      char b = str.charAt(1);

       if (a == b)
       {
          while (str.length() > 1)
          {
             a = str.charAt(0);
             b = str.charAt(1);

             if (a != b) break;

             counter++;
             str = str.substring(1);


          }

           return a + stringClean( str.substring(1) ) ;
       }

    }

    return str.charAt(0) + stringClean (str.substring(1) );

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:19:50+00:00Added an answer on June 15, 2026 at 7:19 am

    My question is the following, is there any way to solve this problem without using a loop and using only recursion. No global variables, no loops.

    Answer: Yes. It is very simple. Try below:

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

    Your CodingBat results in below:

    stringClean(“yyzzza”) → “yza” “yza” OK
    stringClean(“abbbcdd”) → “abcd” “abcd” OK
    stringClean(“Hello”) → “Helo” “Helo” OK
    stringClean(“XXabcYY”) → “XabcY” “XabcY” OK
    stringClean(“112ab445”) → “12ab45” “12ab45” OK
    stringClean(“Hello Bookkeeper”) → “Helo Bokeper” “Helo Bokeper” OK
    other tests OK

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am stuck in this problem, I am calling a webService that returns me
I kind of stuck on this. I have a string like 18=abcd1, 19=jghrt23, 20=outut
I've been stuck on this for 3 days now. I have two pages that
I have been stuck with this problem for quite some time now and i
Stuck with this particular issue. I have code that get lots of pages from
I'm stuck with this problem I have a serie of pages of the CharacterView
I have been stuck with this problem for a while now, and I just
im stuck in this terrible problem, I have a SOAP based webservice implemented in
I'm currently stuck with this problem. Suppose I have the following daily data +-------------------------+-----+----+
I have been stuck on this problem for a very long time. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.