I've assembled this little algorithm that is able to create a list of all possible arrangments of letters and underscores (tea -> te_ -> t_a -> _ea -> t__ ->...) of a set of words. It then compares the arrangments and finds the unique ones (tee & tea => [tea, tee, t_a, t_e, _ea, _ee, __a, __e]). My problem is that for larger words the program is using way too much memory (I'm creating a tree of all the possible combinations after all). I was thinking of creating each words' tree simultaneously and then unsetting the repetitions as it goes along, but I'm not sure how that would be done.
Here's the part of my code that has very poor memory-allocation with the words "tea" and "tee" as test cases:
$word1 = "tea tee";
$word1 = preg_split('/ +/', $word1);
$word1 = array2d($word1);
$word1 = get_multipleCombinations($word1);
$word1 = compare_combinations($word1);
foreach($word1 as $key1=>$level1){
foreach($level1 as $key2=>$level2){
foreach($level2 as $key3=>$level3){
echo $word1[$key1][$key2][$key3];
}
echo " ";
}
}
function array2d($words){
$count = count($words);
for ($i = 0; $i <= $count-1; $i++) {
$words[$i] = str_split($words[$i]);
}
for ($i = 0; $i <= $count-1; $i++) {
for ($j = 0; $j <= count($words[$i])-1; $j++){
$words[$i][$j] = array("_", $words[$i][$j]);
}
}
return $words;
}
function get_combinations($arrays) {
$result = array(array());
foreach ($arrays as $key => $values) {
$temp = array();
foreach ($result as $results) {
foreach ($values as $value) {
$temp[] = array_merge($results, array($key => $value));
}
}
$result = $temp;
}
return $result;
}
function get_multipleCombinations($array){
$count0 = count($array)-1;
for ($i = 0; $i <= $count0; $i++){
$result[$i] = get_combinations($array[$i]);
}
return($result);
}
function compare_combinations($array){
$count = count($array)-1;
for($j = 0; $j <= $count; $j++){
for($z = 0; $z <= $count; $z++){
if($j !== $z){
for($i = 0; $i <= count($array[$j])-1; $i++){
if(count($array[$j]) === count($array[$z]) && $array[$j][$i] === $array[$z][$i]){
$array[$j][$i] = array("");
$array[$z][$i] = array("");
}
}
}
}
}
return($array);
}
Feel free to criticize any dumb parts of the code/question, I'm new to this and it would be very helpful.
Thank you for your time.