Solved

# without 2 challenge

Posted on 2015-01-15
75 Views
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
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
• 18
• 12
• 4

LVL 84

Expert Comment

ID: 40552579
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

LVL 32

Assisted Solution

awking00 earned 150 total points
ID: 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

LVL 7

Author Comment

ID: 40554206
``````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

LVL 32

Expert Comment

ID: 40554297
Use mine.
0

LVL 84

Expert Comment

ID: 40554399
"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

LVL 7

Author Comment

ID: 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;
}
``````

does this looks good? I passed all tests.

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

LVL 84

Expert Comment

ID: 40554437
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

LVL 7

Author Comment

ID: 40567183
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

LVL 84

Expert Comment

ID: 40567197
Thus it differs from http:#a40553579

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

LVL 7

Author Comment

ID: 40570664
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

LVL 7

Author Comment

ID: 40580666
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

LVL 7

Author Comment

ID: 40592023
``````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

LVL 7

Author Comment

ID: 40592038
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

LVL 7

Author Comment

ID: 40592044
``````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

LVL 32

Expert Comment

ID: 40592106
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

LVL 7

Author Comment

ID: 40592299
``````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

LVL 7

Author Comment

ID: 40592307
``````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

LVL 84

Expert Comment

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

LVL 84

Expert Comment

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

LVL 7

Author Comment

ID: 40593678
0

LVL 7

Author Comment

ID: 40593690
``````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

LVL 84

Expert Comment

ID: 40593697
0

LVL 7

Author Comment

ID: 40593705
``````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

LVL 7

Author Comment

ID: 40593710
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

LVL 84

Expert Comment

ID: 40593715
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

LVL 84

Expert Comment

ID: 40593813
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

LVL 7

Author Comment

ID: 40593860
``````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

LVL 84

Accepted Solution

ozo earned 350 total points
ID: 40593879
You have two return statements to account for the cases when the if condition is true, and when the if condition is false.

0

LVL 7

Author Comment

ID: 40594358
``````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

LVL 84

Expert Comment

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

LVL 7

Author Comment

ID: 40595052
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

LVL 84

Expert Comment

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

LVL 32

Expert Comment

ID: 40595653
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

LVL 7

Author Comment

ID: 40595769
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

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

Notify sent to other threads in Java 9 54
spring maven example issues 3 105
troubleshoot a python script 8 114
netbeans can not run self signed app 8 37
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
###### Suggested Courses
Course of the Month4 days, 14 hours left to enroll