Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

pairstar challenge

Posted on 2016-09-21
2
Medium Priority
?
112 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
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 28

Accepted Solution

by:
rrz earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

916 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