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

asked on

hasBad challenge

Hi,

I am working on below challenge

http://codingbat.com/prob/p139075
I wrote my code as below
public boolean hasBad(String str) {

if(str.indexOf("bad",1)!=-1&&str.indexOf("bad",0)!=-1)
return true;
else
return false;
  
}

Open in new window



I am failing below tests.
Expected	Run		
hasBad("badxx") → true	false	X	    
hasBad("xbadxx") → true	true	OK	    
hasBad("xxbadxx") → false	true	X	    
hasBad("code") → false	false	OK	    
hasBad("bad") → true	false	X	    
hasBad("ba") → false	false	OK	    
hasBad("xba") → false	false	OK	    
hasBad("xbad") → true	true	OK	    
hasBad("") → false	false	OK	    
hasBad("badyy") → true	false	X	    
other tests
X	   

Open in new window


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

ASKER

public boolean hasBad(String str) {

for(int i=0;i<=2;i++)
if(str.indexOf("bad",i)!=-1)
{
return true;
}
else
{
return false;
}
return false;
  
}

Open in new window


i changed as above still failing one test case

Expected	Run		
hasBad("badxx") → true	true	OK	    
hasBad("xbadxx") → true	true	OK	    
hasBad("xxbadxx") → false	true	X	    
hasBad("code") → false	false	OK	    
hasBad("bad") → true	true	OK	    
hasBad("ba") → false	false	OK	    
hasBad("xba") → false	false	OK	    
hasBad("xbad") → true	true	OK	    
hasBad("") → false	false	OK	    
hasBad("badyy") → true	true	OK	    
other tests
X	    

Open in new window

SOLUTION
Avatar of dpearson
dpearson

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 hasBad(String str) {


if(str.indexOf("bad",1)==1 ||str.indexOf("bad",0)==0)
{
return true;
}
else
{
return false;
}

  
}

Open in new window

I passed all the tests now.

when we need to put {}
Avatar of dpearson
dpearson

when we need to put {}
If you have 1 line of code after an if statement, then the {} are optional.  But only for 1 line of code.

So

if (x) {
    return true ;
}

is the same as

if (x)
   return true ;

Most people prefer to always put the { } in because these are very different:

if (x) {
    doSomething() ;
    return true ;
}

if (x)
   doSomething() ;
   return true ;    // Whoops - this is the 2nd line after 'if' so it ALWAYS gets called

so this code is actually the same as:

if (x) {
   doSomething() ;
}
return true ;

So adding the {} makes it all much clearer.

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

ASKER

 int i = str.lastIndexOf("bad", 1);
        return i == 0 || i == 1;

what is meaning of above 2 lines.

If lastIndexOf "bad" searching backwards after index 1 finds then return i as 1 or i as 0

I am not very clear. Please advise
i is lastIndexOf "bad" searching backwards from index 1
if it is true that i equals 0 or i equals 1, return true
if it is false that i equals 0 or i equals 1, return false
Another way -
if (str.startsWith("bad") || str.startsWith("bad", 1)) {
 return true;
} else {
return false;
}
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
what is meaning of
int i = str.lastIndexOf("bad", 1);
        return i == 0 || i == 1;
it means the same as
return str.lastIndexOf("bad", 1)==0 || str.lastIndexOf("bad", 1)==1;
//or
if( str.lastIndexOf("bad", 1)==0 || str.lastIndexOf("bad", 1)==1 ){ return true; }else{ return false; }
//or
if( str.lastIndexOf("bad", 1)==0 ){ return true; }
if( str.lastIndexOf("bad", 1)==1 ){ return true; }
return false;
Only just realised that my "lastIndexOf" solution can be improved further still, like this...

public boolean hasBad(String str) {
  return str.lastIndexOf("bad", 1) >= 0;
}

Open in new window

Avatar of gudii9

ASKER

if (str.startsWith("bad") || str.startsWith("bad", 1)) {

Open in new window


i never used startsWith function.


what is the meaning of above line esp str.startsWith("bad", 1). please advise

public boolean hasBad(String str) {
  return str.lastIndexOf("bad", 1) >= 0;
}

Open in new window


How above code pasing the below test case
hasBad("xxbadxx") → false	false	OK

Open in new window

It search for string "bad" backwards starting from index 1 right then searches at index 0 if tit finds returns true which makes sense to me.
But if string is "xxbadxx" if It search for string "bad" backwards starting from index 1 right then searches at index 0 it is not going to find "bad" so it must return false right. please advise
Avatar of gudii9

ASKER

if (str.startsWith("bad") || str.startsWith("bad", 1)) {

Open in new window



above can be written as below right which is more simple to my understanding
if (str.startsWith("bad",0) || str.startsWith("bad", 1)) {

Open in new window


if yes what is difference between below two and when to use which one
if (str.startsWith("bad",0) || str.startsWith("bad", 1)) {

Open in new window

and
if (str.startsWith("bad") || str.startsWith("bad", 1)) {

Open in new window

public boolean startsWith(String prefix,  int toffset)

Tests if the substring of this string beginning at the specified index starts with the specified prefix.
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#startsWith%28java.lang.String,%20int%29
"xxbadxx".lastIndexOf("bad", 1) does not find "bad" before index 1, so it returns -1
-1 >= 0 is false
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