Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

pairstar challenge

Hi,

I am working on below challenge.
http://codingbat.com/prob/p158175
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) {
  
  if(str.length()==0||str.length()==1){
    return str;
  }
  else{
  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
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

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

Open in new window


above passed all tests. any improvements or alternate approaches?
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial