Solved

pairstar challenge

Posted on 2016-09-21
2
60 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 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse java  build path 6 57
servlet  URL Rewriting 1 36
java stored proc example 9 30
eclipse apache tomcat admin console 52 97
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

773 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