Problem:
Given a string, return true if "bad" appears starting at index 0 or 1 in the string, such as with "badxxx" or "xbadxx" but not "xxbadxx". The string may be any length, including 0. Note: use .equals() to compare 2 strings.
hasBad("badxx") → true
hasBad("xbadxx") → true
hasBad("xxbadxx") → false
Solution:
public boolean hasBad(String str) { if(str.length() < 3) return false; else if ((str.substring(0,3)).equals("bad")) return true; else if (str.length() > 3){ if ((str.substring(1,4)).equals("bad")) return true; } return false; }
public boolean hasBad(String str) {
ReplyDeleteString bad = "bad";
if(str.length()<=3 && !str.contains(bad)){
return false;
}
if(str.startsWith(bad) || str.substring(1, 4).startsWith(bad)){
return true;
}
return false;
}
String badStr = "bad";
ReplyDeleteif (str.length() < 3) {
return false;
}
if (str.substring(0, 3).equals(badStr)) {
return true;
}
if ( str.length() > 3 && str.substring(1, 4).equals(badStr)) {
return true;
} else {
return false;
}
}
public boolean hasBad(String str) {
ReplyDeletereturn str.length()>=3?str.substring(0,3).equals("bad")
||
str.length()>3&&str.substring(1,4).equals("bad"):false;
}
public boolean hasBad(String str) {
ReplyDeleteif(str.length()<3) {
return false;
}
if (str.substring(0).startsWith("bad") || str.substring(1).startsWith("bad")){
return true;
}
return false;
}
Good luck!
Deletepublic final boolean hasBad(String str) {
ReplyDeletereturn !str.isEmpty() && (str.startsWith("bad")
|| str.substring(1).startsWith("bad"));
}
public boolean hasBad(String str) {
ReplyDeletereturn ((str.length() >= 3 && str.startsWith("bad"))
|| (str.length() >= 4 && str.substring(1, 4).equals("bad")));
}