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()); } }
public String minCat(String a, String b) {
ReplyDeleteint 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);
}
public String minCat(String a, String b) {
ReplyDeleteif(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;
}
public String minCat(String a, String b) {
ReplyDeleteint 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 "";
}
public String minCat(String a, String b) {
ReplyDeleteString 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;
}
public String minCat(String a, String b) {
ReplyDeleteif(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());
}
}
public String minCat(String a, String b) {
ReplyDeleteif(a.length() <= b.length())
{
return a + b.substring(b.length() - a.length());
}
return a.substring(a.length() - b.length()) + b;
}
public String minCat(String a, String b) {
ReplyDeleteint min=Math.min(a.length(),b.length());
return a.substring(a.length()-min) + b.substring(b.length()-min);
}
I just saw we had the same approach! Lol, this solution is so much shorter and easier to follow.
Deletepublic String minCat(String a, String b) {
ReplyDeleteint size = Math.min(a.length(), b.length());
return a.substring(a.length()- size) + b.substring(b.length()-size);
}
public String minCat(String a, String b) {
ReplyDeleteif(a.length()<b.length()) {
return a + b.substring(b.length() - a.length());
}
return a.substring(a.length()-b.length())+b;
}
public String minCat(String a, String b) {
ReplyDeleteString str = a.length() >= b.length() ? a.substring(a.length()-b.length()) + b : a + b.substring(b.length()-a.length());
return str;
}
public String minCat(String a, String b) {
ReplyDeleteif (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));
}
public String minCat(String a, String b) {
ReplyDeleteint 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 "";
}
public String minCat(String a, String b)
ReplyDelete{
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;
}
public String minCat(String a, String b)
ReplyDelete{
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;
}
public String minCat(String a, String b) {
ReplyDeleteint min=Math.min(a.length(),b.length());
return a.substring(a.length()-min) + b.substring(b.length()-min);
}
public String minCat(String a, String b) {
ReplyDeleteif (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
public String minCat(String a, String b) {
ReplyDeleteint max = Math.max(a.length(),b.length());
return a.substring(max-b.length()) + b.substring(max-a.length());
}
public String minCat(String a, String b)
ReplyDelete{
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;
}
public String minCat(String a, String b) {
ReplyDeleteStringBuilder 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();
}
public String minCat(String a, String b) {
ReplyDeleteif(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);
}