Given two strings, A and B, create a bigger string made of the first char of A, the first char of B, the second char of A, the second char of B, and so on. Any le
You've got a workable approach, but you could significantly simplify it by using a single loop with two counters:
int apos = 0, bpos = 0;
while (apos != a.length() || bpos != b.length()) {
if (apos < a.length()) m += a.charAt(apos++);
if (bpos < b.length()) m += b.charAt(bpos++);
}
In this loop you will "make progress" on each step by advancing apos
, bpos
, or both. Once a string runs out of characters, its corresponding pos
stops advancing. The loop is over when both pos
reach their ends.
Note: When you need to append to a string in a loop, use StringBuilder.
public String mixString(String a, String b) {
String c="";
String left="";
int min;
if(a.length()>b.length()) {
min=b.length();
left=a.substring(b.length(),a.length());
}
else {
min=a.length();
left=b.substring(a.length(),b.length());
}
for(int i=0;i<min;i++) {
c +=a.substring(i,i+1)+b.substring(i,i+1);
}
return c+left;
}
Here is my attempt::
public static String mixString1(String a,String b){
String result="";
int startValue = 0;
int increment = 0;
if(a.length()<=b.length()){
for(int i=0;i<a.length();i++){
result = result+a.charAt(i)+b.charAt(i);
}
}
else if(a.length()>=b.length()){
for(int i=0;i<b.length();i++){
result = result+a.charAt(i)+b.charAt(i);
}
}
if(a.length()<b.length()){
startValue= a.length();
increment = b.length();
}
else {
startValue= b.length();
increment = a.length();
}
if(a.length()<b.length()){
for(int j=startValue;j<increment;j++){
result = result+b.charAt(j);
}
}
else if(a.length()>b.length()){
for(int j=startValue;j<increment;j++){
result = result+a.charAt(j);
}
}
return result;
}
I used Merge sort approach to solve this problem. First I converted both the strings into their respective character arrays and then merged both the arrays and converted the array back to a string. You can find my code below,I have tested the code and it is working. Let me know if you have any questions..
public String merge(String leftStr, String rightStr) {
char[]left = leftStr.toCharArray();
char[]right = rightStr.toCharArray();
int nL = left.length;
int nR= right.length;
char[] mergeArr= new char[nL+nR];
int i=0,j=0,k=0;
int temp =0;
while(i<nL && j<nR){
if (temp==0){
mergeArr[k]=left[i];
i++;
temp =1;
}else{
mergeArr[k]=right[j];
j++;
temp=0;
}
k++;
}
while(i<nL){
mergeArr[k]=left[i]; k++; i++;
}
while(j<nR){
mergeArr[k]=right[j]; k++; j++;
}
return new String(mergeArr);
}
Another approach using StringBuilder
and char[]
. Works as long as the first is longer than the second, which is guaranteed by the first method.
public String mixString(String a, String b) {
if (b.length() > a.length())
return mixString(new StringBuilder(b), a.toCharArray(), 0);
return mixString(new StringBuilder(a), b.toCharArray(), 1);
}
public String mixString(StringBuilder ab, char[] b, int start) {
int i = 0;
for (char c : b)
ab.insert(i++*2 + start, "" + c);
return ab.toString();
}
public String mixString(String a, String b) {
StringBuilder sb = new StringBuilder("");
int longer = 0;
if (a.length()>b.length()) longer = a.length();
else longer = b.length();
for (int i = 0; i <longer;i++){
if (i<a.length()) sb.append(a.substring(i,i+1));
if (i<b.length()) sb.append(b.substring(i,i+1));
}
return sb.toString();
}