-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinCat.java
33 lines (30 loc) · 1.01 KB
/
MinCat.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package String01;
public class MinCat {
/*
* 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.
* */
public String minCat(String a, String b) {
/*
* minCat("Hello", "Hi") → "loHi"
* minCat("Hello", "java") → "ellojava"
* minCat("java", "Hello") → "javaello"
* */
int dif = 0;
if (a.length()==b.length()){
return a + "" + b;
} else {
if (a.length()<b.length()) {
dif = b.length() - a.length();
return a + "" + b.substring(dif);
}
if (a.length()>b.length()) {
dif = a.length() - b.length();
return a.substring(dif) + "" + b;
}
}
return "";
}
}