I have two arrays of characters. They look like this:
1) S ( J D )
2) S J Z D
The second array will always be different then the first. It will not have any parentheses, and will have either +/- 1 alpha character, or just have the position of two characters swapped. I essentially need to merge the two arrays together, such that I have the characters of the second array, but maintaining the parentheses from the first. (Its easier to understand if you look at the test cases down below)
In the above example, the output should be:
S (J Z D)
I'm not quite sure how to do this though. What i've bee toying around with so far:
You can count the alpha characters in each array, and see if you are adding, subtracting or swapping.
For the addition case, I could make a copy of array #1, but without parentheses (so array #3). Compare the this array with array #2, and find the first difference. Note the index. Then iterate through #1 until you hit that index (subtracting 1 for each parentheses). Then copy over the character from #2 into the array.
For the subtraction case, do the same thing as addition, only when you find the difference, just remove it from list #1.
Can anyone think of a better way to handle this?
Test cases:
Input
Array1: A (G F)
Array2: A G D F
Output
A (G D F)
Input
Array1: A (G F)
Array2: A G F D
Output
A (G F) D
Input
Array1: A (G F)
Array2: A D G F
Output
A D (G F)
Input
Array1: A (G F)
Array2: A F
Output
Input
Array1: A (G F)
Array2: G F
Output
(G F)
Input
Array1: A (G F)
Array2: A F G
Output
A (F G)