in reply to Another word puzzle with too many permutations

I got the task as follows: you are given some dictionary, and some string of repeating letters.
The main goal: find unique, matched by all letters words out of the given dictionary and do it fast).
Here is my solution
(it works equally fast on either on the dogs or cars (takes about 0.1s))
@words = qw/toyota dorf honda audi ford/; %hash=('adduhifoorndatoyota' => ''); $unique = 0; foreach $word (@words){ foreach $key (sort keys %hash){ $length = 0; if($key=~m/\#/){$_ = $'} else {$_= $key} foreach $l (split //, $word){if(/$l/){$_ = "$`$'"; $length++;}} if($length == length($word)){ $newKey = '#'.$_; if($key=~m/\#/){ if($newKey eq '#'){$newKey .= $unique++} $hash{$newKey} = $hash{$key}; $hash{$newKey}.= ' '.$word; } else { $hash{$newKey} = $word; } } } } foreach (sort keys %hash){ print $hash{$_},"\n" if /\#\d+/; } -----OUTPUT----- toyota dorf honda audi toyota honda audi ford
if you noticed I intentionally put dorf into dictionary,
so there would be two unique combinations.
Exactly the same works with the dogs.
It takes about 0.1 seconds to finish calculations.
:)
But requires at least two words being mixed in the string line.

Update:

@words = qw/toyota dorf honda audi ford/; %hash=('adduhifoorndatoyota' => ''); $unique = 0; foreach $word (@words){ foreach $key (sort keys %hash){ $length = 0; if($key=~m/\#/){$_ = $'} else {$_= $key} foreach $l (split //, $word){if(/$l/){$_ = "$`$'"; $length++;}} if($length == length($word)){ $newKey = '#'.$_; if($key=~m/\#/){ if($newKey eq '#'){$newKey .= $unique++} $hash{$newKey} = $hash{$key}; $hash{$newKey}.= ' '.$word; } else { $hash{$newKey} = $word; } } } } foreach (sort keys %hash){ print $hash{$_},"\n" if /\#\d+/; } -----OUTPUT----- toyota dorf honda audi toyota honda audi ford

Replies are listed 'Best First'.
Re^2: Another word puzzle with too many permutations
by hdb (Monsignor) on Oct 16, 2013 at 12:51 UTC

    Please restore your code. On my machine it took 72 minutes to find the correct solution for the dog's dataset using your code. The %hash contains around 5,000,000 entries when it is done.

    It is also very similar to something I wrote for Challenge: 8 Letters, Most Words. This I have not (yet) posted as it already runs for 8 days and has only processed 90% of the input. It also generates a hash with possible solutions and is expected to be of the same order of magnitude.