0

I'm trying the exercises in CodingBat.

Java > String-1 > seeColor: Given a string, if the string begins with "red" or "blue" return that color string, otherwise return the empty string.

My running code is:

public String seeColor(String str) {
  int len = str.length();

  if (len >= 3 && str.substring(0, 3).equals("red")) {
   return str.substring(0, 3);
  } else if (len >= 4 && str.substring(0, 4).equals("blue")) {
     return str.substring(0, 4);
    }
  return "";

}

But I found this other answer all over the web:

public String seeColor(String str) {
    int len = str.length();

    if(len >= 4) {
         if(str.substring(0, 4).equals("blue"))
            return "blue";
         else if(str.substring(0, 3).equals("red"))
            return "red";
        else
            return "";
    }   

    else if(len == 3) {
        if(str.substring(0, 3).equals("red"))
            return "red";
        else
            return "";
    }

    else
        return "";
}

My solution has less duplicate code (or it's what I think). But the other solution looks more efficient. It starts from large Strings, and evaluating only the strings with length of 3 characters at the end. Probably this last solution is more able to make easy changes in the future. I would like to read someone's opinion. Thanks.

Edited: I add an apologize, I haven't formatted my code appropriately at first as suggested by @MikeDunlavey.

1 Answer 1

6

I wouldn't do either. I wouldn't assume the second one is more efficient, though it is more complicated. Both create objects needlessly.

public String seeColor(String str) {
    return str.startsWith("red") ? "red" :
           str.startsWith("blue")? "blue" : "";
}

Every time you call substring this creates two objects which is really expensive for what this function is doing.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, that's a notorious improvement. I will keep in mind avoiding object duplication, and look for other methods like startsWith. Thanks for your kind answer, it's my first question.
@EMER: Notice also that Peter's answer is carefully formatted, both for indentation so you can see what's inside of what, and for spacing, so related things are directly above and below each other. Experienced programmers know that such attention to detail really helps when seeing if the code is right, or when it's necessary to change it.
@MikeDunlavey Thanks, I fixed it. That was a conscious mistake, I wasn't sure how it worked the code blocks in this site. But what I didn't know is about where to cut long length statements, and I found your suggestion to put related things above and below each other very helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.