Alternate Print

You have two strings “A” and “B”. Your task is to print these two strings in an alternative fashion according to indices i.e. first character of “A”, the first character of “B”, the second character of “A”, the second character of “B” and so on.

Note:
If all characters of one string are printed and some characters of other string are remaining, then you have to print the remaining characters of the other string at the end of the resulting string.
Follow Up :
Can you solve the problem in O(N) time complexity and O(1) space complexity?
For Example:
A = “abc” B = “fdh” then answer will be afbdch.
Input format :
The first line of input contains a single integer T, representing the number of test cases or queries to be run. 

Then the T test cases follow.

The first line of each test contains two space-separated strings “A” and “B”. 
Output format :
For each test case, print a single line containing these two strings in an alternative fashion according to indices.

Note:

You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10    
1 <= |A|, |B| <= 10^5 
A and B contains only lower case English characters

Time Limit: 1sec
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
CodingNinjas
author
2y

I followed brute force approach
Step 1 : Take a result string
Step 2 : One by one append characters of both given strings in alternate style.
Step 3 : Iterate loop from i=0 to iS...read more

CodingNinjas
author
2y
Recursive Approach

The idea here is to use recursion and append characters one by one from A and B using a variable turn. If turn equals 0 then append character from A and if turn equals 1 then append ...read more

CodingNinjas
author
2y
Iterative Approach

The idea here is to run a loop and append characters from A and B one by one until both of them are not empty, then append the remaining characters of the non-empty string to answer....read more

Add answer anonymously...
Bank of America Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter