Solved

endX challenge

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

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This video teaches viewers about errors in exception handling.
Simple Linear Regression
Suggested Courses

635 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