Skip to content
Advertisement

Zipped String from 2 (or more) Strings – “AB” + “YZ” = “AYBZ”

So I am trying to return another String from 2 input sentences zipped together. If the 2 sentences are identical in length it will produce an actual output. If the two input sentences are not identical in length then it will just return an empty string. Here is my code so far but I can’t figure out how to zip the words correctly could someone help me. BTW it would be nice if you could help me by doing this recursively because I’m trying to practice that.

Ex:

JavaScript
JavaScript

Advertisement

Answer

I love your class name. Sincerely

In order to really “return” it, you could implement something similar to the examples below.

Update/Edit: The original answer is below, as the three new approaches (which don’t care about the number of strings to zip) are on top.


[MultiThreaded]

The ultimate ZIPPER

Each of the words to zip is handled by a thread. Why? Ask yourself: WHY NOT???

Boredom makes this things happen.

Each word will be lovely processed by its own thread. Threads organize themselves in order not to process the same word and set the same positions thanks to the AtomicInteger.

JavaScript

Was this required by any means? OF COURSE NOT. But it was fun, and my girlfriend told me to do it.

Lie, I don’t have a girlfriend. Do you really think I’d be doing this if I had one??


[Zip'em all]

Two different approaches:

1. Direct move

Works regardless of the number of strings to zip, from 2 to n.* This means these approaches are also the replacement of the old methods, as you can either call getStringsZippedDirectMove("ABC,"123") or getStringsZippedDirectMove(yourArray).

In this approach, each string is completely allocated at a time, so each element in the list is only accessed/processed once. The main loop iterates based on the number of elements in the array:

JavaScript

For example, for the first String: “AAA“:

JavaScript

For the last String: “789“:

JavaScript

enter image description here

Same output:

JavaScript

Each iteration leads to the complete relocation of the String element’s chars.

2. Multi move from index – Holger style

Motivated by Holger’s comments

This also will work regardless of the number of strings to zip, from 2 to n.*

JavaScript

The main loop iterates three times, as it’s based on each text’s length (3). At each iteration, it will append the char at position i from each of the Strings in the array in the index marked by j. This last counter increments at each assignation.

enter image description here

JavaScript


Original answer block (2 strings)

Arrays

Double assignation at each iteration

JavaScript

Loop over the length of any of the Strings and insert each element in order. During the iterations, this are the assigned values:

JavaScript

Horrendous paint:

enter image description here

As a result, we got:

zip = ['A','1','B','2','C','3'] ||| new String(zip) = "A1B2C3"

Note: If you don’t love arrays, you have no hearth.

Single assignation at each iteration

This uses another approach for the iteration logic, which seems totally useless when you can do what the previous example does. but just for fun.

JavaScript

String#subString:

JavaScript

Probably the worst performant approach.


String#charAt

Note that charAt(), correctly pointed out on Elliot‘s answer, will not work with this logic; it will give you a numeric text, as result of adding their respective unicode values. It won’t append the characters.

Alternatives to work with charAt() would be using the empty string workaround, or creating a char[] as in the second boring example.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement