without 2 challenge

Hi,

I am trying below challenge
http://codingbat.com/prob/p142247


Given a string, if a length 2 substring appears at both its beginning and end, return a string without the substring at the beginning, so "HelloHe" yields "lloHe". The substring may overlap with itself, so "Hi" yields "". Otherwise, return the original string unchanged.

without2("HelloHe") → "lloHe"
without2("HelloHi") → "HelloHi"
without2("Hi") → ""
'

i did not understand the challenge to proceed further. please advise.

thanks in advance.
LVL 7
gudii9Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ozoConnect With a Mentor Commented:
You have two return statements to account for the cases when the if condition is true, and when the if condition is false.

The unreachable code link was meant to address your "what is meaning of reachable" question.
0
 
ozoCommented:
without2("HelloHe") → "lloHe"  "He" appears at both its beginning and end, so return return a string without the "He" at the beginning,
without2("HelloHi") → "HelloHi"    "He" at the beginning is not the same as the "Hi" at the end,  so return the original string unchanged.
without2("Hi") → ""     "Hi" appears at both its beginning and end, so return return a string without the "Hi" at the beginning.
0
 
awking00Connect With a Mentor Commented:
public String without2(String str) {
  boolean matches = str.substring(0,2).equals(str.substring(str.length() - 2));
  if (matches) {
    return str.substring(0,2);
  }
  return str;
}

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
gudii9Author Commented:
public String without2(String str) {
  int strLen=str.length();
  String strOut=str;
  
  if(strLen>2 && str.substring(0,2).equals(str.substring(strLen-2))){
  
  strOut=str.substring(strLen-2);
  return strOut;
  }
   else{
  
 // strOut=str.substring(strLen-2);
  return strOut;
  }
  //return strOut;
}

Open in new window


Expected	Run		
without2("HelloHe") → "lloHe"	"He"	X	    
without2("HelloHi") → "HelloHi"	"HelloHi"	OK	    
without2("Hi") → ""	"Hi"	X	    
without2("Chocolate") → "Chocolate"	"Chocolate"	OK	    
without2("xxx") → "x"	"xx"	X	    
without2("xx") → ""	"xx"	X	    
without2("x") → "x"	"x"	OK	    
without2("") → ""	""	OK	    
without2("Fruits") → "Fruits"	"Fruits"	OK	    
other tests

Open in new window


i just typed as above and got below errors. please advise how to fix and improve my code
0
 
awking00Commented:
>> please advise how to fix and improve my code <<
Use mine.
0
 
ozoCommented:
"if a length 2 substring appears at both its beginning and end, return a string without the substring at the beginning"
Your code instead returns the substring at the end

Also, your test is incorrect when strLen==2.
0
 
gudii9Author Commented:
public String without2(String str) {
  int strLen=str.length();
  String strOut=str;
  
  if(strLen>=2 && str.substring(0,2).equals(str.substring(strLen-2))){
  
  strOut=str.substring(2,strLen);
  return strOut;
  }
   else{
  
 // strOut=str.substring(strLen-2);
  return strOut;
  }
  //return strOut;
}

Open in new window


does this looks good? I passed all tests.

I wonder how above code different from ID: 40553579 code. please advise
0
 
ozoCommented:
ID: 40554408 differs from ID: 40553579 in correctly using str.substring(2) instead of str.substring(0,2) and in not failing when str.length()<2
0
 
gudii9Author Commented:
ID: 40554408 differs from ID: 40553579 in correctly using str.substring(2) instead of str.substring(0,2)

i do not see str.substring(2). I do find in old post  str.substring(0,2)

40554408 

public String without2(String str) {
  int strLen=str.length();
  String strOut=str;
  
  if(strLen>=2 && str.substring(0,2).equals(str.substring(strLen-2))){
  
  strOut=str.substring(2,strLen);
  return strOut;
  }
   else{
  
 // strOut=str.substring(strLen-2);
  return strOut;
  }
  //return strOut;
}





40553579 



public String without2(String str) {
  boolean matches = str.substring(0,2).equals(str.substring(str.length() - 2));
  if (matches) {
    return str.substring(0,2);
  }
  return str;
}

Open in new window


please advise
0
 
ozoCommented:
Thus it differs from http:#a40553579

str.substring(2)
is the same as
str.substring(2,strLen);
0
 
gudii9Author Commented:
not failing when str.length()<2
i wonder what you mean by
I have my code checking if length >2 righ as below
Please advise
  if(strLen>=2 && str.substring(0,2).equals(str.substring(strLen-2))){

Open in new window


40554408 

public String without2(String str) {
  int strLen=str.length();
  String strOut=str;
  
  if(strLen>=2 && str.substring(0,2).equals(str.substring(strLen-2))){
  
  strOut=str.substring(2,strLen);//same as substring(2) incorrect and not failing when str.length()<2
  return strOut;
  }
   else{
  
 // strOut=str.substring(strLen-2);
  return strOut;
  }
  //return strOut;
}



40553579 



public String without2(String str) {
  boolean matches = str.substring(0,2).equals(str.substring(str.length() - 2));
  if (matches) {
    return str.substring(0,2);
  }
  return str;
}

ID: 40554408 differs from ID: 40553579 in correctly using str.substring(2) instead of str.substring(0,2) and in not failing when str.length()<2

i do not see str.substring(2). I do find in old post  str.substring(0,2) 
Thus it differs from http:#a40553579

str.substring(2)
is the same as
str.substring(2,strLen);

Open in new window

0
 
gudii9Author Commented:
ID: 40554408 differs from ID: 40553579 in correctly using str.substring(2) instead of str.substring(0,2) and in not failing when str.length()<2

can you please elaborate on this comment?
I am not clear.
0
 
gudii9Author Commented:
40554408 

public String without2(String str) {
  int strLen=str.length();
  String strOut=str;
  
  if(strLen>=2 && str.substring(0,2).equals(str.substring(strLen-2))){
  
[b]  strOut=str.substring(2,strLen);[/b]
  return strOut;
  }
   else{
  
 // strOut=str.substring(strLen-2);
  return strOut;
  }
  //return strOut;
}

Open in new window


i see above one incorrectly using str.substring(2,strLen);
0
 
gudii9Author Commented:
But it is passing all the tests though
Expected	Run		
without2("HelloHe") → "lloHe"	"lloHe"	OK	    
without2("HelloHi") → "HelloHi"	"HelloHi"	OK	    
without2("Hi") → ""	""	OK	    
without2("Chocolate") → "Chocolate"	"Chocolate"	OK	    
without2("xxx") → "x"	"x"	OK	    
without2("xx") → ""	""	OK	    
without2("x") → "x"	"x"	OK	    
without2("") → ""	""	OK	    
without2("Fruits") → "Fruits"	"Fruits"	OK	    
other tests
OK

Open in new window


please advise
0
 
gudii9Author Commented:
public String without2(String str) {
  boolean matches = str.substring(0,2).equals(str.substring(str.length() - 2));
  if (matches) {
    return str.substring(0,2);
  }
  return str;
}

Open in new window


infact above one failing many tests

Expected	Run		
without2("HelloHe") → "lloHe"	"He"	X	    
without2("HelloHi") → "HelloHi"	"HelloHi"	OK	    
without2("Hi") → ""	"Hi"	X	    
without2("Chocolate") → "Chocolate"	"Chocolate"	OK	    
without2("xxx") → "x"	"xx"	X	    
without2("xx") → ""	"xx"	X	    
without2("x") → "x"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:2)"	X	    
without2("") → ""	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:2)"	X	    
without2("Fruits") → "Fruits"	"Fruits"	OK	    
other tests

Open in new window


may be i understand your comment in reverse way?
0
 
awking00Commented:
As ozo pointed out, I incorrectly used str.substr(0,2), which would simply return the first two characters when what is needed if there is a match is everything beginning with the third character.
public String without2(String str) {
  boolean matches = str.substring(0,2).equals(str.substring(str.length() - 2));
  if (matches) {
    return str.substring(2);
  }
  return str;
}
0
 
gudii9Author Commented:
public String without2(String str) {
  boolean matches = str.substring(0,2).equals(str.substring(str.length() - 2));
  if (matches) {
    return str.substring(2);
  }
  return str;
}

Open in new window


it is failing few test cases. looks like cornere cases.

Expected	Run		
without2("HelloHe") → "lloHe"	"lloHe"	OK	    
without2("HelloHi") → "HelloHi"	"HelloHi"	OK	    
without2("Hi") → ""	""	OK	    
without2("Chocolate") → "Chocolate"	"Chocolate"	OK	    
without2("xxx") → "x"	"x"	OK	    
without2("xx") → ""	""	OK	    
without2("x") → "x"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:2)"	X	    
without2("") → ""	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:2)"	X	    
without2("Fruits") → "Fruits"	"Fruits"	OK	    
other tests

Open in new window


how to handle them. please advise
0
 
gudii9Author Commented:
public String without2(String str) {
  boolean matches = str.substring(0,2).equals(str.substring(str.length() - 2));
  if (str.length()>=2 && matches) {
    return str.substring(2);
  }else{
  return str;
  }
 // return str;
}

Open in new window


i wrote as above still failing some cases. please advise.

How many times we can write return statements in a method. Can i return one in if other in else other outside the if and also else as above? which infact did not work unless i comment one return.
please advise
0
 
ozoCommented:
str.substring(0,2) does not make sense when str is "" or "x"
0
 
ozoCommented:
You can write as many return statements in a method as you want, as long as all are reachable
0
 
gudii9Author Commented:
what is meaning of reachable. please advise
0
 
gudii9Author Commented:
public String without2(String str) {

if (str.length()>=2){
  boolean matches = str.substring(0,2).equals(str.substring(str.length() - 2));
  if (matches) {
    return str.substring(2);
  }else{
  return str;
  }
  
  
  }
  return str;
}

Open in new window


i passed all tests. I wonder how can i improve my code
Expected	Run		
without2("HelloHe") → "lloHe"	"lloHe"	OK	    
without2("HelloHi") → "HelloHi"	"HelloHi"	OK	    
without2("Hi") → ""	""	OK	    
without2("Chocolate") → "Chocolate"	"Chocolate"	OK	    
without2("xxx") → "x"	"x"	OK	    
without2("xx") → ""	""	OK	    
without2("x") → "x"	"x"	OK	    
without2("") → ""	""	OK	    
without2("Fruits") → "Fruits"

Open in new window

0
 
gudii9Author Commented:
public String without2(String str) {

if (str.length()>=2){
  boolean matches = str.substring(0,2).equals(str.substring(str.length() - 2));
  if (matches) {
    return str.substring(2);
  }else{
  return str;
  }  
  
  }
 // return str;
}

Open in new window


if i comment outer return statement getting below error

Compile problems:


Error:	public String without2(String str) {
	              ^^^^^^^^^^^^^^^^^^^^
This method must return a result of type String

Possible problem: the if-statement structure may theoretically 
allow a run to reach the end of the method without calling return.
Consider adding a last line in the method return some_value; 
so a value is always returned.

see Example Code to help with compile problems

Open in new window


i wonder why it is error. I have anyway supplied return withn if and other return else right?

is it is giving compilation error since i have not handled case where string length less than 2?

please advise
0
 
gudii9Author Commented:
in this example is it is advisable to create String st;
then assign to st in if and else loop then return st rather than returning 3 times separately in if , else outside if, else loops.
please advise
0
 
ozoCommented:
Line 8 is unnecessary, since if you don't return there, the control flow path will reach the return str; in line 13

Without line 8, line 7 becomes unnecessary.

I might use a single && test with no matches temporary, but that's a stylistic preference that others may disagree on, not an improvement.

If you might contemplate other variations of that code that may do other things after line 8 besides  return str;, then the line can serve the use of reminding you which variation is in effect in this case.
0
 
ozoCommented:
in this example is it is advisable to create String st;
I would probably call that a stylistic preference, and leave the choice to the individual programer.

One consideration for such decisions could be that if one is writing several related functions, then if one can build them all with a similar structure, it might make it easier to see the relationships between the functions, and simpler to modify one function into another.
(or if one thinks of the function they are writing as an example of a class of similar functions, then the structure of the program may be a reflection of the more general class of possible programs, and one's choice can be a way to suggest what kinds programs one thinks of as similar, or what modifications one may be contemplating)

For example, although I would probably not choose to use regular expressions for this particular challenge, if I were doing
extrafront challenge
startMatch challenge
without x challenge
without x2 challenge
deFront challenge
without 2 challenge
all at the same time, I might consider using return str.replaceAll(pattern,replacement); for all of them.
0
 
gudii9Author Commented:
public String without2(String str) {

if (str.length()>=2&&str.substring(0,2).equals(str.substring(str.length() - 2))){
//  boolean matches = str.substring(0,2).equals(str.substring(str.length() - //2));
 // if (matches) {
    return str.substring(2);
 // }
  
  }
  return str;
}

Open in new window


i passed all tests as above. I still wonder why i have two return statements above.
How is unreachable code related with return statements which i often get. That link did not explain that specific point. please advise
0
 
gudii9Author Commented:
public String withoutX(String str) {

if(str.length()>=1){
		
int strLen=str.length();
String firstChar=str.substring(0,1).equals("x")?"":str.substring(0,1);
String lastChar=str.substring(strLen-1).equals("x")?"":str.substring(strLen-1);
String rest=str.length() <= 2? "" :str.substring(1,strLen-1);
return firstChar+rest+lastChar;


}



}

Open in new window


i wonder above complains saying (from other challenge question but put here also to make my method return concept 100% concrete


Compile problems:


Error:      public String withoutX(String str) {
                    ^^^^^^^^^^^^^^^^^^^^
This method must return a result of type String

Possible problem: the if-statement structure may theoretically
allow a run to reach the end of the method without calling return.
Consider adding a last line in the method return some_value;
so a value is always returned.

see Example Code to help with compile problems
0
 
ozoCommented:
What would happen in http:#a40594358 in the case when str.length()<1 ?
0
 
gudii9Author Commented:
What would happen in http:#a40594358 in the case when str.length()<1 ?
i am not handling case when str.length()<1.
Flow comes out of if loop and exits method i guess since no else block and no code between end of if block bracket }

and method end bracket }
0
 
ozoCommented:
And does it return a result of type String when it exits?
0
 
awking00Commented:
Let's analyze your code from ID: 40594358 assuming an empty String (i.e a string with a length of 0)
public String withoutX(String str) { ==> This method must return a String value

if(str.length()>=1){ ==> This equals false so none of the next 5 lines are "reached" (i.e. They don't do anything)
            
int strLen=str.length();
String firstChar=str.substring(0,1).equals("x")?"":str.substring(0,1);
String lastChar=str.substring(strLen-1).equals("x")?"":str.substring(strLen-1);
String rest=str.length() <= 2? "" :str.substring(1,strLen-1);
return firstChar+rest+lastChar;

}

==> So nothing is returned, but the method requires returning a String. Therefore you get a compile error.
}
0
 
gudii9Author Commented:
Oh I see  now clearly how I am missing returning string which method expects when length 0.Thanks again for patiently explaining the mistake I did
0
All Courses

From novice to tech pro — start learning today.