Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

changeXy challenge

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
ASKER CERTIFIED SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of phoffric
phoffric

>> 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.
@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.
I'm sorry. I was wrong.   Your choice of the replace method is fine.
Avatar of gudii9

ASKER

any improvements or alternate approaches for this challenge?
Phoffric, told you in the first comment above here.  He asked you a question about line 12 of your code.
Before advising upon improvement, we first need to see your recursive solution to the challenge.
Avatar of gudii9

ASKER

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?
Avatar of gudii9

ASKER

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
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

Avatar of gudii9

ASKER

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