Solved

pairstar challenge

Posted on 2016-09-21
2
92 Views
Last Modified: 2016-09-22
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
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Author Comment

by:gudii9
ID: 41809272
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?
0
 
LVL 27

Accepted Solution

by:
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

0

Featured Post

Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

636 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