Basically, this code does multiplication of chosen operations.
public static String[] gen8String(String[] pattern1, String[] pattern2){
String[] combinedSubset = new String[90]; //emty array for the subset of 90 strings
String combinedString = ""; //string holder for each combined string
int index = 0; //used for combinedSubset array
int present = 0; //used to check if all 6 characters are present
for(int i = 0; i < 15; i++){
for(int j = 0; j < 15; j++){
combinedString = pattern1[i] + pattern2[j]; //combine both 4 letter strings into 8 char length string
char[] parsedString = combinedString.toCharArray(); //parse into array
//check if all 6 characters are present
for(int k = 1; k <= 6; k++)
{
if(new String(parsedString).contains(k+"")) {
present++;
}
else
break;
//if all 6 are present, then add it to combined subset
if(present == 6)
combinedSubset[index++] = combinedString;
}
present = 0;
}
}
return combinedSubset;
}
Let's look at an example:
Let's says I have 2 strings ABCDEF
and ACDEBF
. Now before I call gen8string
, I will first perform a \${6 \choose 4}\$ deletion operation on each string. For instance, I will take ABCDEF
delete any two characters in that string and I am left with substring of length 4. How many different such strings can I have (given all the letters are unique)? \$15 = {6 \choose 4}\$. Now, I will do the same for both strings. Now I am left with 2 string arrays, each of size 15.
In gen8String
, I am passing in the 2 above mentioned arrays and I am combining the substrings under the following constraints:
- I can only combine 2 strings to make one 8-length string (4+4).
- I can only combine them if there are no overlapping missing characters.
What do I mean by the 2nd point? Well, let's say I one of the inputs in pattern
is ABCD
and in pattern2
one of the inputs is ACDE
. These 2 substrings cannot be combined because there are overlapping missing characters, i.e. the F
. However, if I have ABCD
in pattern1
and CDEF
, these can be combined because all 6 characters are present in the 8-length string at least once. So, no overlapping missing characters. This can be seen by tracing through the code.
As a concluding note, this function essentially does a
$${{6}\choose{4}} * {{4}\choose{2}} = 90$$
How can I optimize/generalize this code? Ways to improve it?
gen8String("ACFFG", "XXZ")
do, for example, and why? Also, is this Java? Please tag the question accordingly. \$\endgroup\$