x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 102

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") → ""
'

0
gudii9
• 18
• 12
• 4
2 Solutions

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

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;
}
``````
0

Author 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;
}
``````

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

i just typed as above and got below errors. please advise how to fix and improve my code
0

Commented:
Use mine.
0

Commented:
"if a length 2 substring appears at both its beginning and end, return a string without the substring at the beginning"

Also, your test is incorrect when strLen==2.
0

Author 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;
}
``````

does this looks good? I passed all tests.

I wonder how above code different from ID: 40553579 code. please advise
0

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
0

Author 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;
}
``````

0

Commented:
Thus it differs from http:#a40553579

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

Author Commented:
not failing when str.length()<2
i wonder what you mean by
I have my code checking if length >2 righ as below
``````  if(strLen>=2 && str.substring(0,2).equals(str.substring(strLen-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);//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);
``````
0

Author 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

Author 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;
}
``````

i see above one incorrectly using str.substring(2,strLen);
0

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

0

Author 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;
}
``````

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

may be i understand your comment in reverse way?
0

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

Author 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;
}
``````

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

0

Author 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;
}
``````

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

Commented:
str.substring(0,2) does not make sense when str is "" or "x"
0

Commented:
You can write as many return statements in a method as you want, as long as all are reachable
0

Author Commented:
0

Author 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;
}
``````

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"
``````
0

Commented:
0

Author 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;
}
``````

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

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?

0

Author 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.
0

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

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

Author 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;
}
``````

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

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

0

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

}

}
``````

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

Commented:
What would happen in http:#a40594358 in the case when str.length()<1 ?
0

Author 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

Commented:
And does it return a result of type String when it exits?
0

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

Author 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.