Java > String-1 > deFront (CodingBat Solution)

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;
}


32 comments :

  1. public String deFront(String str) {
    String result = str.substring(2,str.length());
    if (str.charAt(1)=='b') result = "b" + result;
    if (str.charAt(0)=='a') result = "a" + result;
    return result;

    }

    ReplyDelete
    Replies
    1. good solution. but still lacks some mechanism to prevent "IndexOutOfBoundsException" when the string has 1 or less length.
      in 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.

      Delete
    2. public String deFront(String str) {

      if(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;
      }

      Delete
    3. public String deFront(String str) {

      if(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;
      }

      Delete
  2. public String deFront(String str) {
    String 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?

    ReplyDelete
    Replies
    1. 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).
      this 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."

      Delete
  3. hi. the article's proposed solution involves loop, which is not the context of the exercise. so, the comment solution is the better one.

    ReplyDelete
  4. public String deFront(String str) {

    if (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 "";
    }

    ReplyDelete
  5. public String deFront(String str)
    {
    //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
    }

    ReplyDelete
  6. public String deFront(String str) {

    String 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;
    }

    ReplyDelete
  7. public String deFront(String str) {

    String 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;
    }

    ReplyDelete
  8. public String deFront(String str) {

    if((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);
    }

    ReplyDelete
  9. if ((str.substring(0, 1).equals("a")) &&
    (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());

    ReplyDelete
  10. public String deFront(String str) {
    if(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());
    }

    ReplyDelete
  11. public String deFront(String str) {
    if(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;
    }

    ReplyDelete
  12. boolean aAtFirst = str.charAt(0) == 'a';
    boolean 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;

    ReplyDelete
  13. public String deFront(String str) {
    boolean 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;
    }

    ReplyDelete
  14. public String deFront(String str) {

    if(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);

    }

    ReplyDelete
  15. public String deFront(String str) {
    if (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;
    }

    ReplyDelete
  16. public String deFront(String str) {
    if(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);
    }

    ReplyDelete
  17. good question spent more than two hours to solve it

    ReplyDelete
  18. public String deFront(String str) {
    if(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;
    }

    ReplyDelete
  19. public String deFront(String str) {
    if(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);
    }

    ReplyDelete
  20. String result = "";
    int 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;

    ReplyDelete
  21. public String deFront(String str) {
    if(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());
    }

    ReplyDelete
  22. public String deFront(String str) {
    if(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());
    }

    ReplyDelete
  23. public String deFront(String str){
    String result = str.substring(2);
    if (str.charAt(1)=='b') result = "b" + result;
    if (str.charAt(0)=='a') result = "a" + result;
    return result;}

    ReplyDelete
  24. public String deFront(String str)
    {
    String data = "";

    if(str.charAt(0) == 'a')
    data += 'a';

    if(str.charAt(1) == 'b')
    data += 'b';


    data += str.substring(2);
    return data;
    }

    ReplyDelete
  25. Another way

    public 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);
    }

    ReplyDelete

  26. 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"

    ReplyDelete
  27. public String deFront(String str) {
    if (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);

    }

    ReplyDelete
  28. public String deFront(String str) {
    if (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);
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact