Java > String-1 > minCat (CodingBat Solution)

Problem:

Given two strings, append them together (known as "concatenation") and return the result. However, if the strings are different lengths, omit chars from the longer string so it is the same length as the shorter string. So "Hello" and "Hi" yield "loHi". The strings may be any length.

minCat("Hello", "Hi") → "loHi"
minCat("Hello", "java") → "ellojava"
minCat("java", "Hello") → "javaello"


Solution:

public String minCat(String a, String b) {
  if (a.length() == b.length())
    return a+b;
  if (a.length() > b.length()){
    int diff = a.length() - b.length();
    return a.substring(diff, a.length()) + b;

  } else {
    int diff = b.length() - a.length();
    return a + b.substring(diff, b.length());
  }
    
}


12 comments :

  1. public String minCat(String a, String b) {
    int lengthA = a.length();
    int lengthB = b.length();
    if (lengthA>lengthB) {
    //return a.substring(b.length())+b;
    a = a.substring(lengthA-lengthB);
    }
    else if (lengthB>lengthA) {
    b = b.substring(lengthB-lengthA);
    }
    return a.concat(b);
    }

    ReplyDelete
  2. public String minCat(String a, String b) {
    if(a.length() > b.length())
    return a.substring(a.length()-b.length()) + b;
    else if(a.length() < b.length())
    return a + b.substring(b.length()-a.length());
    return a.substring(a.length()-2) + b;
    }

    ReplyDelete
  3. public String minCat(String a, String b) {
    int aLen = a.length();
    int bLen = b.length();

    if(aLen>bLen && aLen != 0 && bLen != 0)
    return a.substring(aLen-bLen,aLen)+b;
    else if(bLen>aLen && aLen != 0 && bLen != 0)
    return a+b.substring(bLen-aLen,bLen);
    else return "";
    }

    ReplyDelete
  4. public String minCat(String a, String b) {
    String result="";
    if(a.length()<b.length()){
    result+= a+b.substring(b.length()-a.length(),b.length());
    }
    if(b.length()<a.length()){
    result+= a.substring(a.length()-b.length(),a.length())+b;
    }
    if(a.length()==b.length()){
    result+= a+b;
    }
    return result;
    }

    ReplyDelete
  5. public String minCat(String a, String b) {
    if(a.length() > b.length())
    {
    return a.substring(a.length()-b.length(),a.length())+b;
    }
    else
    {
    return a+b.substring(b.length()-a.length(),b.length());
    }

    }

    ReplyDelete
  6. public String minCat(String a, String b) {
    if(a.length() <= b.length())
    {
    return a + b.substring(b.length() - a.length());
    }
    return a.substring(a.length() - b.length()) + b;

    }

    ReplyDelete
  7. public String minCat(String a, String b) {
    int min=Math.min(a.length(),b.length());
    return a.substring(a.length()-min) + b.substring(b.length()-min);
    }

    ReplyDelete
    Replies
    1. I just saw we had the same approach! Lol, this solution is so much shorter and easier to follow.

      Delete
  8. public String minCat(String a, String b) {
    int size = Math.min(a.length(), b.length());
    return a.substring(a.length()- size) + b.substring(b.length()-size);

    }

    ReplyDelete
  9. public String minCat(String a, String b) {
    if(a.length()<b.length()) {
    return a + b.substring(b.length() - a.length());
    }
    return a.substring(a.length()-b.length())+b;


    }

    ReplyDelete
  10. public String minCat(String a, String b) {
    String str = a.length() >= b.length() ? a.substring(a.length()-b.length()) + b : a + b.substring(b.length()-a.length());
    return str;
    }

    ReplyDelete
  11. public String minCat(String a, String b) {
    if (a.length() == b.length()) {
    return a + b;
    }
    int differenceA = a.length() - b.length();
    int differenceB = b.length() - a.length();
    return (a.length() > b.length()) ? (a.substring(differenceA) + b) : (a + b.substring(differenceB));
    }

    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