Java > String-2 > endOther (CodingBat Solution)

Problem:

Given two strings, return true if either of the strings appears at the very end of the other string, ignoring upper/lower case differences (in other words, the computation should not be "case sensitive"). Note: str.toLowerCase() returns the lowercase version of a string.

endOther("Hiabc", "abc") → true
endOther("AbC", "HiaBc") → true
endOther("abc", "abXabc") → true


Solution:

public boolean endOther(String a, String b) {
  a = a.toLowerCase();
  int aLen = a.length();
  
  b = b.toLowerCase();
  int bLen = b.length();
  
  if (aLen < bLen) {
    String temp = b.substring(bLen - aLen, bLen);
    if (temp.compareTo(a) == 0)
      return true;
    else
      return false;
    
  } else {
    String temp = a.substring(aLen - bLen, aLen);
    if (temp.compareTo(b) == 0)
      return true;
    else
      return false;
  }
  
}


24 comments :

  1. public boolean endOther(String a, String b) {
    String aLow = a.toLowerCase();
    String bLow = b.toLowerCase();
    if (aLow.endsWith(bLow) || bLow.endsWith(aLow)) {
    return true;
    }
    return false;
    }

    ReplyDelete
    Replies
    1. You can go easier:

      public boolean endOther(String a, String b) {
      String aLower = a.toLowerCase();
      String bLower = b.toLowerCase();

      return bLower.endsWith(aLower) || aLower.endsWith(bLower);
      }

      Delete
    2. great but ,think about the time complexity,already a string has given in method signature,so there is no need to declare another string ,as it will take a heap area ,a little more space.

      Delete
    3. Reply to Rhino Horn, you can go even easier:
      public boolean endOther(String a, String b) {
      a = a.toLowerCase();
      b = b.toLowerCase();
      return a.endsWith(b) || b.endsWith(a);
      }

      Delete
  2. //return (a.toLowerCase().endsWith(b.toLowerCase())
    // || b.toLowerCase().endsWith(a.toLowerCase()));

    ReplyDelete
    Replies
    1. public boolean endOther(String a, String b) {
      return b.toLowerCase().endsWith(a.toLowerCase()) || a.toLowerCase().endsWith(b.toLowerCase());
      }

      Delete
  3. public boolean endOther(String a, String b) {

    int lenA = a.length();
    int lenB = b.length();

    int min = Math.min(lenA,lenB);

    String catA = a.substring(lenA-min).toLowerCase();
    String catB = b.substring(lenB-min).toLowerCase();

    return catA.equals(catB);

    }

    ReplyDelete
  4. public boolean endOther(String a, String b) {
    a=a.toLowerCase();
    b=b.toLowerCase();
    return (a.endsWith(b)||b.endsWith(a));
    }

    ReplyDelete
  5. This is my solution :


    public boolean endOther(String a, String b) {
    a=a.toLowerCase();
    b=b.toLowerCase();

    if(a.length()==b.length()) {
    if(a.contains(b) || b.contains(a)) return true;
    }

    if(a.length()>=b.length()) {

    if(a.substring(Math.abs(a.length()-b.length())).equalsIgnoreCase(b)) return true;
    }

    if(a.length()<=b.length()) {

    if(b.substring(Math.abs(a.length()-b.length())).equalsIgnoreCase(a)) return true;
    }
    return false;
    }

    ReplyDelete
    Replies
    1. imagine the Time complexity and space complexity

      Delete
  6. public boolean endOther(String a, String b) {
    int LongerStr = (a.length()>b.length())? a.length():b.length();
    for ( int i = 0; ia.length()){
    if(b.toLowerCase().substring(b.length()-(a.length()), b.length()).equals(a.toLowerCase())) return true;
    }else{
    if(a.toLowerCase().substring(a.length()-(b.length()), a.length()).equals(b.toLowerCase())) return true;

    }


    }
    return false;
    }

    you can call me a traditionalist, or die hard make it harder for your self kinda person XD LOL, but it passed the test.

    ReplyDelete
  7. no need to traverse the entire string ,because of this your time complexity is O(n),it should be O(1)

    ReplyDelete
  8. public boolean endOther(String a, String b) {
    if (a.length() > b.length()) {
    return a.substring(a.length() - b.length()).toLowerCase().equals(b.toLowerCase());
    }
    return b.substring(b.length() - a.length()).toLowerCase().equals(a.toLowerCase());
    }

    ReplyDelete
  9. public boolean endOther(String a, String b) {
    return a.toLowerCase().endsWith(b.toLowerCase()) ||b.toLowerCase().endsWith(a.toLowerCase());
    }

    ReplyDelete
  10. public boolean endOther(String a, String b) {
    return a.toLowerCase().endsWith(b.toLowerCase())||b.toLowerCase().endsWith(a.toLowerCase());
    }

    ReplyDelete
  11. public boolean endOther(String a, String b) {
    return a.toLowerCase().endsWith(b.toLowerCase()) || b.toLowerCase().endsWith(a.toLowerCase());
    }

    ReplyDelete
  12. public boolean endOther(String a, String b) {
    return b.toLowerCase().endsWith(a.toLowerCase())||a.toLowerCase().endsWith(b.toLowerCase());}

    ReplyDelete
  13. public boolean endOther(String a, String b) {
    a=a.toLowerCase();
    b=b.toLowerCase();
    return ((a.substring(a.length()-1)).equals(b.substring(b.length()-1)));
    }

    ReplyDelete
  14. public boolean endOther(String a, String b)
    {
    if( a.length()>b.length() && (a.substring(a.length()-b.length())).equalsIgnoreCase(b))
    {
    return true;
    }
    else if( b.length()>a.length() && (b.substring(b.length()-a.length())).equalsIgnoreCase(a))
    {
    return true;
    }
    else if(a.equals(b))
    {
    return true;
    }
    return false;
    }

    ReplyDelete
  15. public boolean endOther(String a, String b) {
    return (a.toLowerCase().endsWith(b.toLowerCase())||b.toLowerCase().endsWith(a.toLowerCase()));
    }

    ReplyDelete
  16. public boolean endOther(String a, String b) {
    while(a.substring(a.length()-2,a.length()-1 ).equals(b.substring(b.length()-2,b.length()-1)))
    {
    if (a.endsWith(a.toLowerCase()) || b.endsWith(b.toLowerCase())) {
    return true;
    } else
    return false;
    }
    return false;

    }


    ReplyDelete
  17. shorter way is by: public boolean endOther(String a, String b) {
    String A = a.toLowerCase(), B = b.toLowerCase();
    boolean newA = A.endsWith(B);
    boolean newB = B.endsWith(A);
    return newA || newB;
    }

    ReplyDelete
  18. if(a.length()>b.length()){
    return a.substring(a.length()-b.length()).equalsIgnoreCase(b);
    }else{
    return b.substring(b.length()-a.length()).equalsIgnoreCase(a);
    }

    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