in reply to Re^2: finding tuples
in thread finding tuples

its not exactly a bruteforce, but it can't have a straight answer either. any algorithm that solve this kind of problem will use heuristics, you can't do better than this. here is my try, didn't tried my algorithm against the others, don't know which is faster, anyway
while(<>) { chomp; $s = length $_; @bs = split //; $bucket{$_}++ foreach(@bs); while(keys %bucket) { $tuple = ""; %old_b = { %bucket }; foreach(sort keys(%bucket)) { last if( length $tuple == 4); if($bucket{$_} >= 4 && length $tuple==0) { $tuple = $_ x 4; $bucket{$_}-=4; last; } else { if($bucket{$_} > 0) { $tuple .= $_; $bucket{$_}--; } else { last; } } } if(length $tuple == 4){ print "$tuple\n"; } else { %bucket = %old_b; } foreach( keys(%bucket)){ (delete $bucket{$_}) if ($bucket{$_} +== 0); } } }