in reply to ideas needed for finding matching characters in 2d array
The naïve solution uses four nested loops, two (row+col) to find characters to match, then two (row+col) to find a pairing. That's rather expensive.
A cheaper solution would track the location of each character.
my @a = map { [ split // ] } qw( book reboot rocket ); my %locs; for my $i (0..$#a) { my $row = $a[$i]; for my $j (0..$#$row) { push @{ $locs{$a[$i][$j]} }, "$i,$j"; } } for my $c (sort keys %loc) { next if @{ $loc{$c} } <= 1; print("$c: ", join(' ', @{ $locs{$c} }), "\n"); }
|
|---|