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


21 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
  12. public String minCat(String a, String b) {
    int x=a.length()-b.length();
    int y=b.length()-a.length();
    if(x>y){
    return a.substring(x,a.length())+b;
    }if(x<y){
    return a+b.substring(y,b.length());
    }return "";

    }

    ReplyDelete
  13. public String minCat(String a, String b)
    {
    String str="";
    int alen=a.length();
    int blen = b.length();

    if(alen>blen)
    {
    str=a.substring((a.length()-b.length()), a.length()) + b.substring(0,b.length());
    }
    else
    {
    str= a.substring(0,a.length()) + b.substring((b.length()-a.length()), b.length() );
    }

    return str;
    }

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

    if (b.length() > a.length())
    return a+b.substring(b.length()-a.length());

    return a+b;
    }

    ReplyDelete
  15. 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
  16. public String minCat(String a, String b) {
    if (b.length()<a.length())
    return a.substring(a.length()-b.length(),a.length()) +b;
    return a +b.substring(b.length()-a.length(),b.length());
    }

    //simple coing

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

    }

    ReplyDelete
  18. public String minCat(String a, String b)
    {
    String smaller = a;

    if(b.length() < smaller.length())
    smaller = b;

    String data = "";

    if(smaller.equals(a))
    data += a + b.substring(b.length() - smaller.length() );
    else if(smaller.equals(b))
    data += a.substring(a.length() - smaller.length()) + b;

    return data;
    }

    ReplyDelete
  19. public String minCat(String a, String b) {
    StringBuilder newStr = new StringBuilder();

    if(a.length() > b.length()){
    newStr.append(a.substring(a.length()-b.length(),a.length()) + b);
    }

    if(b.length() > a.length()){
    newStr.append(a + b.substring(b.length()-a.length(),b.length()));
    }

    return newStr.toString();
    }

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

    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