1

Hoping to learn better ways to solve this algorithm. The input consists of two strings.

Input - example 1:

var a = 'abc'
var b = 'def'

Expected Output

mergedString = 'adbecf';

My solution:

var a = 'abc';
var b = 'efg';

function mergeStr(a, b) {
  var aArr = a.split('');
  var bArr = b.split('');
  var newStr = '';

  for (var i = 0; i < aArr.length; i++) {
    newStr += aArr[i] + bArr[i];
  }
}

mergeStr(a, b);

The solution works for the input example above. But I was stuck when the second input values were given to me which is:

var a = 'ab';
var b = 'efg'; 

The expected output is:

aebfg

LOL since I was being timed I came up with the following junk. I just added an if statement to deal with the exact use case I was given. Obviously this solution is junk. I would really like to see what others would do.

function mergeStr(a, b) {
  var aArr = a.split('');
  var bArr = b.split('');
  var newStr = '';

  for (var i = 0; i < aArr.length; i++) {
    newStr += aArr[i] + bArr[i];
  }

  if (a.length < b.length) {
    newStr += b[2];
  }

  console.log(newStr);
}

4 Answers 4

3

An alternative using somewhat fewer comparisons in contrast to the other (current) answers.

function mergeStr( str1, str2 ) {
  let merged = "";
  const min = Math.min( str1.length, str2.length );

  // first part: take from both strings
  for( let i=0; i<min; i++ ) {
    merged += str1[i] + str2[i];
  }

  // second part: take the rest from either string
  let largerStr= str1.length > str2.length ? str1 : str2;
  merged += largerStr.substr( min );

  return merged;
}

This takes the shorter length and basically uses your approach for the first few characters. Afterwards, it just appends the rest of the larger string.

Sign up to request clarification or add additional context in comments.

2 Comments

Good approach like in merge/quick sort when the values does not need to be swapped anymore ;)
This is a nice function. Thanks.
1

You can access each string character like an array, you don't need to use split them to do this.

My solution uses the ternary operator:

function merge( str1, str2 ) {
    let merged = "";
    let max = str1.length > str2.length ? str1.length : str2.length;
    for ( let i = 0; i < max; i++ ) {
        merged += ( str1[i] ? str1[i] : "" ) + ( str2[i] ? str2[i] : "" );
    }
    return merged;
}

// prints acbd
console.log( merge( "ab", "cd") );

// str1 is bigger, prints acbdx
console.log( merge( "abx", "cd") );

// str1 is bigger, prints acbdxy
console.log( merge( "abxy", "cd") );

// str2 is bigger, prints acbdx
console.log( merge( "ab", "cdx") );

// str2 is bigger, prints acbdxy
console.log( merge( "ab", "cdxy") );

1 Comment

Yeah thats a nice function. Scales with all the use cases I tested. Thanks.
1

I like one liners ..

function algo(str1,str2) {
    return Array.from(str1.length >= str2.length ? str1 : str2).map( (v,i) => (str1[i]||"")+(str2[i]||"") ).join("")
}

2 Comments

couldn't you use the index parameter of the map() callback instead of i?
trueeeeee hahaha
0

How about finding the max length variable, then looping through it and if undefined fallback to a empty string.

var a = 'ab';
var b = 'efg';

function mergeStr(a, b) {
  let newStr = '';
  let len = Math.max(a.length - 1, b.length - 1);
  for (let i = 0; i <= len; i++) {
    newStr += (a[i] || "") + (b[i] || "");
  }
  return newStr;
}

console.log(mergeStr(a, b));

1 Comment

You don't need to split and use let instead of var ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.