Solved

changeXy challenge

Posted on 2016-09-15
13
107 Views
Last Modified: 2016-09-18
Hi,

I am working on below challenge
http://codingbat.com/prob/p101372

Psedo code:
1. if array length is zero return 0
2. if array length is 1 and it is x return x
3. if array length is 1 and it is not x return same str
4. else find location of x and replace with y if greater than length
5. return replaced string
I wrote my code as below

public String changeXY(String str) {
  if(str.length()==0){
    return "";
  }
  if(str.length()==1&&"x".equals(str)){
    return "y";
  }
   if(str.length()==1&&!"x".equals(str)){
    return str;
  }
   if(str.length()>1){
    return str.replace('x','y');
  }
  
  
  return null;
}

Open in new window




I am  passing all tests



How to improve/modify my design, code and any other 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
  • 5
  • 4
  • 3
13 Comments
 
LVL 32

Accepted Solution

by:
phoffric earned 250 total points
ID: 41800001
The instructions say you have to do this challenge recursively.
What would happen if you modified your code to just call replace without all those if statements?
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 250 total points
ID: 41800182
0
 
LVL 32

Expert Comment

by:phoffric
ID: 41800281
>> No recursion is necessary for this challenge.
 You are already stating what the author has observed.
 But the challenge is to learn how to use recursion.
0
 Watch the Recording: Learning MySQL 5.7

MySQL 5.7 has a lot of new features. If you've dabbled with an older version of MySQL, it is definitely worth learning.

 
LVL 27

Expert Comment

by:rrz
ID: 41800354
@phoffric,  gudii9 has already gone through 11 recursion challenges last week.  He will not learn anything by using recursion when it is not useful.  He really needs to learn the API better.
0
 
LVL 27

Expert Comment

by:rrz
ID: 41801654
I'm sorry. I was wrong.   Your choice of the replace method is fine.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41803096
any improvements or alternate approaches for this challenge?
0
 
LVL 27

Expert Comment

by:rrz
ID: 41803106
Phoffric, told you in the first comment above here.  He asked you a question about line 12 of your code.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 41803208
Before advising upon improvement, we first need to see your recursive solution to the challenge.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41804068
public String changeXY(String str) {
    if (str.length() == 0){ 
      return str;
      
    }
   else if (str.charAt(0) == 'x') {
     return "y" + changeXY(str.substring(1));
     
   }
   else{ 
     return str.charAt(0) + changeXY(str.substring(1));
     
   }
}

Open in new window

above passes all tests. any improvements or alternate approaches?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41804071
public String changeXY(String str) {
  String regex="x";
  String substr="y";
      String str2 = str.replaceAll(regex, substr);   
      return str2;
}

Open in new window


above also passed all tests
0
 
LVL 27

Expert Comment

by:rrz
ID: 41804107
Good work!
Here is my code.
public String changeXY(String str) {
  int index = str.indexOf("x");
  if(index == -1)return str;
  return changeXY(str.substring(0, index) + "y" + str.substring(index + 1));
}

Open in new window

 
public String changeXY(String str) {
  return str.replaceAll("x", "y");   
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41804114
i like index method approach where u are finding index of x then substring just until  x and concatinating with y(replacement of x) then concatenating later part of string as it is.

also your regex approach of replaceAll is more short
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
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 …
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

627 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