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; } }
public boolean endOther(String a, String b) {
ReplyDeleteString aLow = a.toLowerCase();
String bLow = b.toLowerCase();
if (aLow.endsWith(bLow) || bLow.endsWith(aLow)) {
return true;
}
return false;
}
Great answer
DeleteYou can go easier:
Deletepublic boolean endOther(String a, String b) {
String aLower = a.toLowerCase();
String bLower = b.toLowerCase();
return bLower.endsWith(aLower) || aLower.endsWith(bLower);
}
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.
DeleteReply to Rhino Horn, you can go even easier:
Deletepublic boolean endOther(String a, String b) {
a = a.toLowerCase();
b = b.toLowerCase();
return a.endsWith(b) || b.endsWith(a);
}
public boolean endOther(String a, String b) {
DeleteString alower = a.toLowerCase();
String blower = b.toLowerCase();
boolean result = false;
if (alower==blower)
result = true;
else if (alower.endsWith(blower))
result = true;
else if (blower.endsWith(alower))
result = true;
return result;
}
//return (a.toLowerCase().endsWith(b.toLowerCase())
ReplyDelete// || b.toLowerCase().endsWith(a.toLowerCase()));
public boolean endOther(String a, String b) {
Deletereturn b.toLowerCase().endsWith(a.toLowerCase()) || a.toLowerCase().endsWith(b.toLowerCase());
}
public boolean endOther(String a, String b) {
ReplyDeleteint 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);
}
public boolean endOther(String a, String b) {
ReplyDeletea=a.toLowerCase();
b=b.toLowerCase();
return (a.endsWith(b)||b.endsWith(a));
}
This is my solution :
ReplyDeletepublic 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;
}
imagine the Time complexity and space complexity
Deletepublic boolean endOther(String a, String b) {
ReplyDeleteint 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.
no need to traverse the entire string ,because of this your time complexity is O(n),it should be O(1)
ReplyDeletepublic boolean endOther(String a, String b) {
ReplyDeleteif (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());
}
public boolean endOther(String a, String b) {
ReplyDeletereturn a.toLowerCase().endsWith(b.toLowerCase()) ||b.toLowerCase().endsWith(a.toLowerCase());
}
public boolean endOther(String a, String b) {
ReplyDeletereturn a.toLowerCase().endsWith(b.toLowerCase())||b.toLowerCase().endsWith(a.toLowerCase());
}
public boolean endOther(String a, String b) {
ReplyDeletereturn a.toLowerCase().endsWith(b.toLowerCase()) || b.toLowerCase().endsWith(a.toLowerCase());
}
public boolean endOther(String a, String b) {
ReplyDeletereturn b.toLowerCase().endsWith(a.toLowerCase())||a.toLowerCase().endsWith(b.toLowerCase());}
public boolean endOther(String a, String b) {
ReplyDeletea=a.toLowerCase();
b=b.toLowerCase();
return ((a.substring(a.length()-1)).equals(b.substring(b.length()-1)));
}
public boolean endOther(String a, String b)
ReplyDelete{
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;
}
public boolean endOther(String a, String b) {
ReplyDeletereturn (a.toLowerCase().endsWith(b.toLowerCase())||b.toLowerCase().endsWith(a.toLowerCase()));
}
public boolean endOther(String a, String b) {
ReplyDeletewhile(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;
}
shorter way is by: public boolean endOther(String a, String b) {
ReplyDeleteString A = a.toLowerCase(), B = b.toLowerCase();
boolean newA = A.endsWith(B);
boolean newB = B.endsWith(A);
return newA || newB;
}
if(a.length()>b.length()){
ReplyDeletereturn a.substring(a.length()-b.length()).equalsIgnoreCase(b);
}else{
return b.substring(b.length()-a.length()).equalsIgnoreCase(a);
}