Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

hasBad challenge

Posted on 2014-12-04
16
Medium Priority
?
158 Views
Last Modified: 2014-12-23
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
0
Comment
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
  • Learn & ask questions
  • 5
  • 5
  • 2
  • +2
16 Comments
 
LVL 7

Author Comment

by:gudii9
ID: 40481395
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

0
 
LVL 28

Assisted Solution

by:dpearson
dpearson earned 500 total points
ID: 40481397
You're very close.

The problem is that str.indexOf(x) looks for anywhere that x appears in str.

But you want to make sure that x appears in specific positions - in this case position 0 and position 1.
So you want something more like this:

public boolean hasBad(String str) {

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

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40481766
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 {}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Expert Comment

by:dpearson
ID: 40482082
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
0
 
LVL 36

Assisted Solution

by:mccarl
mccarl earned 500 total points
ID: 40482182
This is how I would first think to do it...
    public boolean hasBad(String str) {
        int i = str.indexOf("bad");
        return i == 0 || i == 1;
    }

Open in new window


And then, upon considering it further and how to make it as optimized as possible (that I can think of, at least) I would do this...
    public boolean hasBad2(String str) {
        int i = str.lastIndexOf("bad", 1);
        return i == 0 || i == 1;
    }

Open in new window



If you look at the solution on the codingbat.com website, it states that their solution is faster just because it doesn't have to search through the whole string. However, upon actually testing their assertion, using indexOf is actually faster except if the input string gets quite long. However, using lastIndexOf eliminates this issue and so it is faster than either their solution of the indexOf solution and doesn't change depending on the length of the input.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40482808
 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
0
 
LVL 84

Expert Comment

by:ozo
ID: 40482880
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
0
 
LVL 32

Expert Comment

by:awking00
ID: 40483206
Another way -
if (str.startsWith("bad") || str.startsWith("bad", 1)) {
 return true;
} else {
return false;
}
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 500 total points
ID: 40483224
Since the challenge mentions using .equals(), you could also do something like this -
if (str.substring(0,3).equals("bad") || str.substring(1,4).equals("bad")) {
return true;
} else {
return false;
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 40483452
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;
0
 
LVL 36

Expert Comment

by:mccarl
ID: 40492777
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

0
 
LVL 7

Author Comment

by:gudii9
ID: 40494869
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
0
 
LVL 7

Author Comment

by:gudii9
ID: 40494878
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

0
 
LVL 84

Expert Comment

by:ozo
ID: 40494904
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 40494913
"xxbadxx".lastIndexOf("bad", 1) does not find "bad" before index 1, so it returns -1
-1 >= 0 is false
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 40494923
str.startsWith("bad",0)
and
str.startsWith("bad")
are the same
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question