I have two arrays of strings, not necessarily of the same length, I want to find all the possible "sets" of combinations between two values from the arrays, without repeats from either array.
For example, given the arrays:
{ "A1", "A2", "A3" }
{ "B1", "B2" }
The result I want is the following sets:
{ ("A1", "B1"), ("A2", "B2") }
{ ("A1", "B1"), ("A3", "B2") }
{ ("A1", "B2"), ("A2", "B1") }
{ ("A1", "B2"), ("A3", "B1") }
{ ("A2", "B1"), ("A3", "B2") }
{ ("A2", "B2"), ("A3", "B1") }
My general direction is to create recursive function that takes as a parameter the two arrays and removes each "chosen" strings at a time, calling itself until either array is empty, however I'm kinda worried about performance issues (I need to run this code on about a 1000 pairs of string arrays).
Can anyone direct my towards an efficient method to do this?
It might be beneficial to think of the two arrays as sides of a table:
A1 A2 A3
---+-------+-------+-------+
B1 | B1,A1 | B1,A2 | B1,A3 |
---+-------+-------+-------+
B2 | B2,A1 | B2,A2 | B2,A3 |
---+-------+-------+-------+
This implies a loop nested within another, one loop for the rows and the other for the columns. This will give you the initial set of pairs:
{B1,A1} {B1,A2} {B1,A3} {B2,A1} {B2,A2} {B2,A3}
Then it is a matter of building up the combinations of that initial set. You can visualise the combinations similarly, with the set of pairs for both the rows and columns:
B1,A1 B1,A2 B1,A3 B2,A1 B2,A2 B2,A3
-----+-----+-----+-----+-----+-----+-----+
B1,A1| | X | X | X | X | X |
-----+-----+-----+-----+-----+-----+-----+
B1,A2| | | X | X | X | X |
-----+-----+-----+-----+-----+-----+-----+
B1,A3| | | | X | X | X |
-----+-----+-----+-----+-----+-----+-----+
B2,A1| | | | | X | X |
-----+-----+-----+-----+-----+-----+-----+
B2,A2| | | | | | X |
-----+-----+-----+-----+-----+-----+-----+
B2,A3| | | | | | |
-----+-----+-----+-----+-----+-----+-----+
Again this can be accomplished with a pair of nested loops (hint: your inner loop's range will be determined by the outer loop's value).
Your problem is equivalent to the following problem:
Problem Statement:
Given two vectors A
with size n
, B
with size m
, where n <= m
.A = [0, 1, 2, ..., n - 1]
.B = [0, 1, 2, ..., m - 1]
.
Find all possible injective and non-surjective mappings from A
to B
.
Solution:
As the size of A is smaller, in one mapping, the number of correspondences is equal to the size of A, i.e., n.
Then we generate all the possible permutations of B, so that the beginning n elements in each permutation can have an one to one correspondence with the elements in A.
The first several permutations and mappings go as follows:
Implementation:
class Helper {
public:
/**
* @brief generateArray
* @param size
* @return A vector [0, 1, ..., size - 1]
*/
vector<int> generateArray(int size) {
vector<int> arr;
for (int i = 0; i < size; ++i) {
arr.push_back(i);
}
return arr;
}
/**
* @brief generateMatches
* @param n, cardinality of the vector X, where X = [0,1, ..., n - 1].
* @param m, cardinality of the vector Y, where Y = [0,1, ..., m - 1].
* @return All possible injective and non-surjective mappings
* from the smaller vector to the larger vector.
*/
vector<vector<pair<int, int> > > generateMatches(int n, int m) {
// Deal with n > m. Swap back when generating pairs.
bool swapped = false;
if (n > m) {
swapped = true;
swap(n, m);
}
// Now n is smaller or equal to m
vector<int> A = generateArray(n);
vector<int> B = generateArray(m);
vector<vector<pair<int, int> > > matches;
// Generate all the permutations of m
do {
vector<pair<int, int> > match;
for (int i = 0; i < n; ++i) {
pair<int, int> p;
if (swapped) {
// Swap back to the original order.
p = make_pair(A[i], B[i]);
} else {
p = make_pair(B[i], A[i]);
}
match.push_back(p);
}
matches.push_back(match);
// Generate next permutation.
} while(next_permutaion(B.begin(), B.end()));
return matches;
}
};
very simple way is
string[] arr = new string[3];
string[] arr1 = new string[4];
string[] jointarr = new string[100];
for (int i = 0; i < arr.Length; i++)
{
arr[i] = "A" + (i + 1);
}
for (int i = 0; i < arr1.Length; i++)
{
arr1[i] = "B" + (i + 1);
}
int k=0;
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr1.Length; j++)
{
jointarr[k] = arr[i] + " " + arr1[j];
k++;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With