0

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) 
user1652427
  • 697
  • 2
  • 8
  • 21
  • 4
    Can you explain what you mean by the `|` and the parentheses? I find this confusing. – MightyPork Aug 07 '13 at 15:32
  • More context would make this problem easier to solve. –  Aug 07 '13 at 15:35
  • Can you show some sample inputs and expected outputs? Also, please describe the format of the data. The function of `|`, `(`, and `)` is not clear. Also, what is `S`? – Vivin Paliath Aug 07 '13 at 16:03

3 Answers3

1
function myFunction()
{
var a = ["S","(","J","D",")"];
var b = ["S","P"];
var c = new Array();

var i = 0;
var j = 0;
var k = 0;

while (i < a.length) {
    if (a[i] == '(' || a[i] == ')')
        c[k++] = a[i++];

    if (i < a.length && j < b.length) {
        // If character in a and b matches the add to the final result
        if (a[i] == b[j]) {
            c[k++] = a[i++];
            j++;
        } else {
                // If the character in a and b don't match then check if character in a exist in b. 
                // If yes then add the character in b else skip
                if (b.indexOf(a[i]) != -1)
                    c[k++] = b[j++];
                else
                    i++;
            }
        }
    }

    while (j < b.length)
        c[k++] = b[j++];

alert(c);
}
Rohit Bansod
  • 181
  • 2
  • 2
0
package com.test;

import java.util.ArrayList;
import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
    Character[] a = {'S','(','J','D',')'};
    Character[] b = {'S','J','Z','D'};
    ArrayList<Character> c = new ArrayList<Character>();

    int i = 0; // Array a pointer
    int j = 0; // Array b pointer

        while (i < a.length) {
            // If it is an opening or closing bracket then add to the final result
            if (a[i] == '(' || a[i] == ')')
                c.add(a[i++]);

            if (i < a.length && j < b.length) {
            // If character in a and b matches the add to the final result
            if (a[i] == b[j]) {
                c.add(a[i++]);
                j++;
            } else {
                // If the character in a and b don't match then check if character in a exist in b. 
                // If yes then add the character present in b to final result else skip
                if (Arrays.asList(b).contains(a[i]))
                    c.add(b[j++]);
                else
                    i++;
            }
        }
    }

    while (j < b.length)
        c.add(b[j++]);

    System.out.println(c);
}

}

Rohit Bansod
  • 181
  • 2
  • 2
0

Can anyone think of a better way to handle this?

I think it would be easier to copy array #2 (which has all the characters you want in the result) and then insert the parenthesis from array #1 into the copy.

var array1 = "S(JZD)".split(''),
    array2 = "SZD".split('');

var result = array2.slice();
for (var i=array1.length; i--; ) // iterate backwards to prevent index troubles
    if (/[()]/.test(array1[i]))
        result.splice(i, 0, array1[i]);
console.log(result);
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • What's the expected result for that input? Btw, you might want to use `.split(/\s*/)` for those strings. – Bergi Aug 07 '13 at 18:26
  • The expected output is A, (, B, F, C, ) – user1652427 Aug 07 '13 at 18:30
  • OK, seems I hadn't understand your requirements. The examples in your question looked as if you only cared for the *position* of the parenthesis. – Bergi Aug 07 '13 at 18:47