Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

endX challenge

Posted on 2016-09-21
2
Medium Priority
?
149 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

664 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