Solved

endX challenge

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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…

708 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

14 Experts available now in Live!

Get 1:1 Help Now