#!/usr/bin/perl -w

use strict;

my @AoA = (
    ['a','b','c'],
    ['a','b','c'],
    ['a','b','d'],
    ['a','b','d'],
    [ qw(a b c) ],
    [ qw(c b a) ],
    [ qw(b c a) ],
    [ qw(a b d) ],
);

my %combinations;
feedme(@AoA);


sub feedme {
	my %uniques;
	
	for my $ary ( @_ ) {
		my $sample = join '', @$ary;
		
		if ( exists $combinations{ $sample } ) {
			++$uniques{ $combinations{ $sample } };
		} else {
			fill_combinations( @$ary );
			++$uniques{ $combinations{ $sample } };
		}
	}
	
	for (keys %uniques) {
		printf "%s found %d times\n", $_, $uniques{ $_ };
	}
}

sub fill_combinations(@) {
	# Note: this function needs to be reworked for >3 elements in an array
	my $combination = join '', @_;
	
	my $index = 0;
	while ( $index < @_ ) {
		my @left = split $_[$index], $combination;
		my $fw = join '', $_[$index], @left;
		my $rv = join '', $_[$index], scalar reverse @left;
		$combinations{ $fw } = $combination;
		$combinations{ $rv } = $combination;
		$index++;
	}
}
