pairstar challenge

Posted on 2016-09-21
Last Modified: 2016-09-22

I am working on below challenge.
Psedo code:
1. check the array length 0  or 1 then retun same str
2.else return string with * in between for repeated pair of characters

I wrote my code as below and not passing all tests
public String pairStar(String str) {
    return str;
  return str.charAt(0)+"*"+pairStar(str.substring(1));


Open in new window

Expected      Run            
pairStar("hello") → "hel*lo"      "h*e*l*l*o"      X      
pairStar("xxyy") → "x*xy*y"      "x*x*y*y"      X      
pairStar("aaaa") → "a*a*a*a"      "a*a*a*a"      OK      
pairStar("aaab") → "a*a*ab"      "a*a*a*b"      X      
pairStar("aa") → "a*a"      "a*a"      OK      
pairStar("a") → "a"      "a"      OK      
pairStar("") → ""      ""      OK      
pairStar("noadjacent") → "noadjacent"      "n*o*a*d*j*a*c*e*n*t"      X      
pairStar("abba") → "ab*ba"      "a*b*b*a"      X      
pairStar("abbba") → "ab*b*ba"      "a*b*b*b*a"      X      
other tests

Any improvements or alternate approaches?      

please advise
Question by:gudii9

Author Comment

ID: 41809272
public String pairStar(String str) {
    return str;
  else if(str.charAt(0)==str.charAt(1)){
  return str.charAt(0)+"*"+pairStar(str.substring(1));
  return str.charAt(0)+pairStar(str.substring(1));

Open in new window

above passed all tests. any improvements or alternate approaches?
LVL 27

Accepted Solution

rrz earned 500 total points
ID: 41809535
Your code is good. Personally, I would clean it up a little.
public String pairStar(String str) {
  if(str.length() < 2){
    return str;
  if(str.charAt(0) == str.charAt(1)){
    return str.charAt(0) + "*" + pairStar(str.substring(1));
  return str.charAt(0) + pairStar(str.substring(1));

Open in new window


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 74
Java Loop 6 48
javap bin 2 22
even odd program using while loop 3 19
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

3 Experts available now in Live!

Get 1:1 Help Now