Problem:
Given a string, return a version without the first 2 chars. Except keep the first char if it is 'a' and keep the second char if it is 'b'. The string may be any length. Harder than it looks.
deFront("Hello") → "llo"
deFront("java") → "va"
deFront("away") → "aay"
Solution:
public String deFront(String str) { int len = str.length(); String temp = ""; for (int i = 0; i < len; i++) { if (i == 0 && str.charAt(i) == 'a') temp += 'a'; else if (i == 1 && str.charAt(i) == 'b') temp += 'b'; else if (i > 1) temp += str.charAt(i); } return temp; }
public String deFront(String str) {
ReplyDeleteString result = str.substring(2,str.length());
if (str.charAt(1)=='b') result = "b" + result;
if (str.charAt(0)=='a') result = "a" + result;
return result;
}
good solution. but still lacks some mechanism to prevent "IndexOutOfBoundsException" when the string has 1 or less length.
Deletein the exercice it says "The string may be any length.", but actually they only provide strings with 2 or more characters long in the test battery. so, your solution works for this, and does not involves any loop.
public String deFront(String str) {
Deleteif(str.substring(0,1).equals("a")&&str.substring(1,2).equals("b"))
{
return str;
}
else if(str.substring(0,1).equals("a"))
{
return str.substring(0,1)+str.substring(2);
}
else if(str.substring(1,2).equals("b"))
{
return str.substring(1);
}
else if(str.length()>=2)
{
return str.substring(2);
}
else
return str;
}
public String deFront(String str) {
Deleteif(str.substring(0,1).equals("a")&&str.substring(1,2).equals("b"))
{
return str;
}
else if(str.substring(0,1).equals("a"))
{
return str.substring(0,1)+str.substring(2);
}
else if(str.substring(1,2).equals("b"))
{
return str.substring(1);
}
else if(str.length()>=2)
{
return str.substring(2);
}
else
return str;
}
public String deFront(String str) {
ReplyDeleteString result = str.substring(2,str.length());
if (str.charAt(1)=='b') result = "b" + result;
if (str.charAt(0)=='a') result = "a" + result;
return result;
}
Can anyone explain to me why this doesn't return an out of bounds error for a string that's two characters long?
because if the string has 2 characters, the substring will return a empty string since the begin index (inclusive) is the same as the end index (exclusve).
Deletethis is from the String API: "IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex."
hi. the article's proposed solution involves loop, which is not the context of the exercise. so, the comment solution is the better one.
ReplyDeletepublic String deFront(String str) {
ReplyDeleteif (str.length()==1&&str.equals("a")){return "a";}
else if (!(str.substring(0,1).equals("a")||str.substring(1,2).equals("b"))){return str.substring(2);}
else if (str.substring(0,1).equals("a")&&str.substring(1,2).equals("b")){return "ab"+str.substring(2);}
else if (str.substring(0,1).equals("a")){return "a"+str.substring(2);}
else if (str.substring(1,2).equals("b")){return "b"+str.substring(2);}
else return "";
}
public String deFront(String str)
ReplyDelete{
//this 2 String vars are meant to "boolean" the return
//so below the if's statements will assign the "true/false"
//I covered some other extra conditions
String a = "";
String b = "";
if (str.length() <= 0) //if 0, nothing to check
return "";
if (str.substring(0,1).equals("a")) //check if (0,1) = "a"
a = "a";
if (str.length() == 1) //avoid checking 1,2 outofbounds
return a;
if (str.substring(1,2).equals("b")) // check if(1,2) = "b"
b = "b";
if (str.length() == 2) //if 2, no need to substring
return a + b;
return a + b + str.substring (2, str.length()); //default
}
public String deFront(String str) {
ReplyDeleteString a=str.substring(0,1);
String b=str.substring(1,2);
if(str.startsWith("a") && !b.equals("b")) return "a" +str.substring (2);
else if(!str.startsWith("a") && b.equals("b")) return str.substring(1);
else if( !str.startsWith("a") && !b.equals("b")) return str.substring(2);
return str;
}
public String deFront(String str) {
ReplyDeleteString a=str.substring(0,1);
String b=str.substring(1,2);
if(str.startsWith("a") && !b.equals("b")) return "a" +str.substring (2);
else if(!str.startsWith("a") && b.equals("b")) return str.substring(1);
else if( !str.startsWith("a") && !b.equals("b")) return str.substring(2);
return str;
}
public String deFront(String str) {
ReplyDeleteif((str.charAt(0)=='a')&&(str.charAt(1)=='b'))
return str;
if(str.charAt(0)=='a')
return 'a'+str.substring(2);
if(str.charAt(1)=='b')
return 'b'+str.substring(2);
return str.substring(2);
}
if ((str.substring(0, 1).equals("a")) &&
ReplyDelete(str.substring(1, 2).equals("b")))
return str.substring(0, str.length());
if (str.substring(0, 1).equals("a"))
return "a" + str.substring(2, str.length());
if (str.substring(1, 2).equals("b"))
return "b" + str.substring(2, str.length());
else
return str.substring (2, str.length());
public String deFront(String str) {
ReplyDeleteif(str.startsWith("ab")){
return str;
}
if(str.charAt(0) == 'a'){
return String.valueOf(str.charAt(0)).concat(str.substring(2,str.length()));
}
if(str.charAt(1) == 'b'){
return String.valueOf(str.charAt(1)).concat(str.substring(2,str.length()));
}
return str.substring(2,str.length());
}
public String deFront(String str) {
ReplyDeleteif(str.length() >= 2) {
boolean a = str.charAt(0) == 'a';
boolean b = str.charAt(1) == 'b';
if(a && b)
return str;
if(a && !b)
return "a" + str.substring(2);
if(!a && b)
return "b" + str.substring(2);
if(!a && !b)
return str.substring(2);
}
else {
if(str.length() == 1) {
if(str.charAt(0) == 'a') return "a";
if(str.charAt(0) == 'b') return "b";
}
}
return str;
}
boolean aAtFirst = str.charAt(0) == 'a';
ReplyDeleteboolean bAtSecond = str.charAt(1) == 'b';
if (!aAtFirst && !bAtSecond)
return str.substring(2);
else if (!bAtSecond)
return str.substring(0,1).concat(str.substring(2));
else if (!aAtFirst)
return str.substring(1);
else
return str;
public String deFront(String str) {
ReplyDeleteboolean existA = false;
boolean existB = false;
String a = "a";
String b = "b";
if (str.length() == 0) {
return str;
}
if (str.charAt(0) == 'a') {
existA=true;
}
if (str.charAt(1) == 'b') {
existB=true;
}
String str2 = str.substring(2,str.length());
if (existA && existB) {
return a+b+str2;
}
if (existA && !existB) {
return a+str2;
}
if (!existA && existB) {
return b+str2;
}
if(!existA && !existB) {
return str2;
}
return str;
}
public String deFront(String str) {
ReplyDeleteif(str.charAt(0)=='a' && str.charAt(1)!='b'){
return str.substring(0,1) + str.substring(2);
}
if(str.charAt(0)!='a' && str.charAt(1)=='b'){
return str.substring(1);
}
if(str.startsWith("ab")){
return str;
}
return str.substring(2);
}
public String deFront(String str) {
ReplyDeleteif (str.substring(0,2).equals("ab")) {
return str;
}
String result = str.substring(2);
if (str.charAt(0) == 'a') {
return "a" + result;
}
if (str.charAt(1) == 'b') {
return "b" + result;
}
return result;
}
public String deFront(String str) {
ReplyDeleteif(str.startsWith("a") && str.startsWith("ab"))
return str;
else if (str.startsWith("a"))
return str.substring(0,1)+ str.substring(2);
else if(str.substring(1,2).equals("b"))
return str.substring(1,2)+ str.substring(2);
return str.substring(2);
}
good question spent more than two hours to solve it
ReplyDeletepublic String deFront(String str) {
ReplyDeleteif(str.charAt(0)=='a'&&str.charAt(1)=='b'){
return str;
}
else if(str.charAt(0)=='a' &&str.charAt(1)!='b'){
return str.charAt(0)+str.substring(2,str.length());
}
else if(str.charAt(0)!='a' &&str.charAt(1)=='b'){
return str.substring(1,str.length());
}
else if(str.charAt(0)!='a' &&str.charAt(1)!='b'){
return str.substring(2,str.length());
}
return str;
}
public String deFront(String str) {
ReplyDeleteif(str.substring(0,1).equals("a")&&str.substring(1,2).equals("b"))
return str;
else if(str.startsWith("a"))
return "a"+str.substring(2);
else if (str.substring(1,2).equals("b"))
return str.substring(1);
else
return str.substring(2);
}
String result = "";
ReplyDeleteint len = str.length();
if(len == 0) return result;
if(len >= 1 && str.charAt(0) == 'a')
result += str.charAt(0);
if(len >= 2 && str.charAt(1) == 'b')
result += str.charAt(1);
result += str.substring(2);
return result;
public String deFront(String str) {
ReplyDeleteif(str.charAt(0)=='a' && str.charAt(1)=='b')
return str;
if(str.charAt(0)=='a')
return str.charAt(0)+str.substring(2,str.length());
if(str.charAt(1)=='b')
return str.charAt(1)+str.substring(2,str.length());
else return str.substring(2,str.length());
}
public String deFront(String str) {
ReplyDeleteif(str.charAt(0)=='a' && str.charAt(1)=='b')
return str;
if(str.charAt(0)=='a')
return "a"+str.substring(2,str.length());
if(str.charAt(1)=='b')
return "b"+str.substring(2,str.length());
else return str.substring(2,str.length());
}
public String deFront(String str){
ReplyDeleteString result = str.substring(2);
if (str.charAt(1)=='b') result = "b" + result;
if (str.charAt(0)=='a') result = "a" + result;
return result;}
public String deFront(String str)
ReplyDelete{
String data = "";
if(str.charAt(0) == 'a')
data += 'a';
if(str.charAt(1) == 'b')
data += 'b';
data += str.substring(2);
return data;
}
Another way
ReplyDeletepublic String deFront(String str) {
if (str.startsWith("ab")) return str;
if (str.startsWith("a")) return "a" + str.substring(2);
if (str.substring(1, 2).equals("b")) return "b" + str.substring(2);
return str.substring(2);
}
ReplyDeleteGiven a string, return a version without the first 2 chars. Except keep the first char if it is 'a' and keep the second char if it is 'b'. The string may be any length. Harder than it looks.
deFront("Hello") → "llo"
deFront("java") → "va"
deFront("away") → "aay"
public String deFront(String str) {
ReplyDeleteif (str.substring(0,2).equals("ab"))
return "ab" + str.substring(2);
if (str.substring(1,2).equals("b"))
return "b" + str.substring(2);
if (str.substring(0,1).equals("a"))
return "a" + str.substring(2);
return str.substring(2);
}
public String deFront(String str) {
ReplyDeleteif (str.startsWith("ab")){
return str;
}
if(str.charAt(0) == 'a'){
return "a"+str.substring(2);
} else if (str.charAt(1) == 'b'){
return "b"+str.substring(2);
}
return str.substring(2);
}