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

asked on

frontAgain challenge

Hi,

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

I wrote my code as below
public boolean frontAgain(String str) {
int len=str.length();
if(str.substring(0,1)==str.substring(len-1) &&str.substring(1,2)==str.substring(len-2,len-1))
{
return true;
}
  else
  {
  return false;
  }
}

I am failing with below message
Expected      Run            
frontAgain("edited") → true      false      X         
frontAgain("edit") → false      false      OK         
frontAgain("ed") → true      false      X         
frontAgain("jj") → true      false      X         
frontAgain("jjj") → true      false      X         
frontAgain("jjjj") → true      false      X         
frontAgain("jjjk") → false      false      OK         
frontAgain("x") → false      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 2 (line number:3)      X         
frontAgain("") → false      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 1 (line number:3)      X         
frontAgain("java") → false      false      OK         
frontAgain("javaja") → true      false      X         
other tests
X         
Your progress graph for this problem

How to fix and improve my code. Thanks in advance
Avatar of ozo
ozo
Flag of United States of America image

Your code attempts to access non-existent parts of strings of length 0 or 1
Your code attempts to compare strings with ==
SOLUTION
Avatar of Sathish David  Kumar N
Sathish David Kumar N
Flag of India image

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 gudii9

ASKER

public boolean frontAgain(String str) {
int len=str.length();

if(len>=4){
if(str.substring(0,1).equals(str.substring(len-1)) &&str.substring(1,2).equals(str.substring(len-2,len-1)))
{
return true;
}
  else
  {
  return false;
  }
  }
  return false;
}

i fixed == part. still failing some test cases. please advise
Your code would return true for "deited" not "edited"
Your code returns false for any string shorter than length 4
Avatar of gudii9

ASKER

i wonder what i should do with 2 character strings.


Given a string, return true if the first 2 chars in the string also appear at the end of the string, such as with "edited".

frontAgain("edited") → true
frontAgain("edit") → false
frontAgain("ed") → true

frontAgain("ed") → true
I wonder how above statement should yield to true based on the challenge description. please advise
Avatar of gudii9

ASKER

public boolean frontAgain(String str) {
int len=str.length();

if(len>=4){
if(str.substring(0,1).equals(str.substring(len-1)) &&str.substring(1,2).equals(str.substring(len-2,len-1)))
{
return true;
}
  else(len<2)
  {
  return false;
  }
  }
  return false;
}

Open in new window


I wonder why i cannot give len<2 condition inside the else block. please advise
Avatar of gudii9

ASKER

public boolean frontAgain(String str) {
int len=str.length();

if(len>=4){
if(str.substring(0,1).equals(str.substring(len-1)) &&str.substring(1,2).equals(str.substring(len-2,len-1)))
{
return true;
}
}

if(len<=2)
  {
  return true;
  }
  
  
  return false;
}

Open in new window


i passed few other tests still failing some. please advise.
challenge did not mention about str being "" or single character.
please advise
frontAgain("ed") → true
What are the first two characters of "ed"?
What are the last two character of "ed"?
Are they the same?
This is how if-then-else statements are constructed http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
Avatar of gudii9

ASKER

else do not take condition where as if and else if takes.

 if (testscore >= 90) {
            grade = 'A';
        } else if (testscore >= 80) {
            grade = 'B';
        } else if (testscore >= 70) {
            grade = 'C';
        } else if (testscore >= 60) {
            grade = 'D';
        } else {
Avatar of gudii9

ASKER

frontAgain("ed") → true
What are the first two characters of "ed"?
What are the last two character of "ed"?
Are they the same?

oh i see from both sides ed so both same.

what happens if only one character is there
Avatar of gudii9

ASKER

Your code would return true for "deited" not "edited"

if(str.substring(0,1).equals(str.substring(len-1)) &&str.substring(1,2).equals(str.substring(len-2,len-1))

i am comparing first with last character and second with last but one character.  I  wonder what i s the mistake i am doing to make java think as deited
please advise
Can a string with one character satisfy "the first 2 chars in the string also appear at the end of the string"?
Avatar of gudii9

ASKER

public boolean frontAgain(String str) {
int len=str.length();

if(len>=4){
if(str.substring(0,1).equals(str.substring(len-2,len-1)) &&str.substring(1,2).equals(str.substring(len-1)))
{
return true;
}
}

if(len<=2)
  {
  return true;
  }
  
  
  return false;
}

Open in new window


i modified as above still failing 3 test cases as below
Expected	Run		
frontAgain("edited") → true	true	OK	    
frontAgain("edit") → false	false	OK	    
frontAgain("ed") → true	true	OK	    
frontAgain("jj") → true	true	OK	    
frontAgain("jjj") → true	false	X	    
frontAgain("jjjj") → true	true	OK	    
frontAgain("jjjk") → false	false	OK	    
frontAgain("x") → false	true	X	    
frontAgain("") → false	true	X	    
frontAgain("java") → false	false	OK	    
frontAgain("javaja") → true	true	OK	    
other tests
OK	    

Open in new window

please advise
Avatar of gudii9

ASKER

Can a string with one character satisfy "the first 2 chars in the string also appear at the end of the string"?
i vote more for No since one character string do not have two characters in it
Granted there is some room for doubt, but the examples
frontAgain("x") → false
frontAgain("") → false

seem to clarify it.
Avatar of gudii9

ASKER

public boolean frontAgain(String str) {
int len=str.length();

if(len>2){
if(str.substring(0,1).equals(str.substring(len-2,len-1)) &&str.substring(1,2).equals(str.substring(len-1)))
{
return true;
}
}

if(len==2)
  {
  return true;
  }
  
  
  return false;
}

Open in new window


i think i got what you mentioned.
I passed all tests.
How can i improve my code. please advise
You're doing pretty good now, but there is a simpler way to do the test with one .equals instead of two

You might also combine some other things
Why compare first character with next to last and second character with last? Just compare the two-character substrings -
public boolean frontAgain(String str) {
  int len = str.length();
  if (len > 1) {
    return str.substring(0,2).equals(str.substring(len - 2));
  }
  return false;
}
As ozo suggests, the above code could be combined to make it shorter -
public boolean frontAgain(String str) {
  if (str.length() > 1) {
    return str.substring(0,2).equals(str.substring(str.length() - 2));
  }
  return false;
}
Two .substring operations instead of four is an improvement, but you can also reduce it to one:
   return str.length()>=2 && str.endsWith(str.substring(0,2));

If you expect length==2 to be common, it may improve efficiency to add
str.length()==2 ||
Avatar of gudii9

ASKER

Two .substring operations instead of four is an improvement, but you can also reduce it to one:
   return str.length()>=2 && str.endsWith(str.substring(0,2));

If you expect length==2 to be common, it may improve efficiency to add
str.length()==2 ||

Open in new window


I wonder how to do this
If you have no reason to expect any particular kind of input to be more common than any other kind of input then it makes little difference.
But if you expect your function to be called more often with one kind of input more often than it is called with other kinds of input, and you have a choice of which kinds of input would flow through the more efficient execution path, then you'd probably prefer to give priority to the kinds of input you expect to see most often.

Here there is no reason for expectation, but there is a choice.

I make the observation that in the case of strings of length 2, you know that str.endsWith(str.substring(0,2)) will automatically be true.
In one sense, it can me more efficient to let the special case be handled as just another instance of the general case,
which may reduce complicated proliferations of branching decisions.
But in another sense, testing the length of a string is a simpler operation than creating a new substring and then checking whether it matches the start of a string.
Avatar of gudii9

ASKER

I make the observation that in the case of strings of length 2, you know that str.endsWith(str.substring(0,2)) will automatically be true.
In one sense, it can me more efficient to let the special case be handled as just another instance of the general case,
which may reduce complicated proliferations of branching decisions.
But in another sense, testing the length of a string is a simpler operation than creating a new substring and then checking whether it matches the start of a string.

both options seems logicaly valid not sure which one to prefer?
please advise
Avatar of gudii9

ASKER

public boolean frontAgain(String str) {
    return str.length()>=2 && str.endsWith(str.substring(0,2));
}

Open in new window


i think i did not understood above code. I see all test passed.


what is meaning of below code.

return str.length()>=2 && str.endsWith(str.substring(0,2));

please advise
ASKER CERTIFIED 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
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