Solved

endX challenge

Posted on 2016-09-21
2
102 Views
Last Modified: 2016-09-22
Hi,

I am working on below challenge.
http://codingbat.com/prob/p105722
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 endX(String str) {
  CharSequence cs="x";
  if(!str.contains(cs)){
    return str;
  }
  return null;
}

Open in new window


I am failing some tests.
Expected      Run            
endX("xxre") → "rexx"      null      X      
endX("xxhixx") → "hixxxx"      null      X      
endX("xhixhix") → "hihixxx"      null      X      
endX("hiy") → "hiy"      "hiy"      OK      
endX("h") → "h"      "h"      OK      
endX("x") → "x"      null      X      
endX("xx") → "xx"      null      X      
endX("") → ""      ""      OK      
endX("bxx") → "bxx"      null      X      
endX("bxax") → "baxx"      null      X      
endX("axaxax") → "aaaxxx"      null      X      
endX("xxhxi") → "hixxx"      null      X      
other tests
X      

Any improvements or alternate approaches?      

please advise
0
Comment
Question by:gudii9
  • 2
2 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 500 total points
ID: 41810201
>> 1. check the array length 0  or 1 then retun same str
Where is that in your code???????

>> 2.else return string with * in between for repeated pair of characters
You're mixing up with another challenge. Why don't you try to do one by one?

This works:
public String endX(String str) {
  if (str.length()==0 || str.length()==1) {
    return str;
  }
  
  if (str.startsWith("x")) {
     return endX(str.substring(1)) + "x";
  }
  return str.substring(0,1) + endX(str.substring(1));
}

Open in new window


Try to find out for yourself why.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41810214
Again,
if (str.length()==0 || str.length()==1) {

Open in new window

can be replaced with
if (str.length()<2) {

Open in new window

0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

This is about my first experience with programming Arduino.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This video teaches viewers about errors in exception handling.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

830 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