Solved

endX challenge

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Scripting vs. Programming languages 25 152
Hide vba in gp 7 83
MySqlDump not dumping triggers 1 18
how to install java on RHEL image on EC2 4 7
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now