Count the number of each letter you have (minus blanks). Count the number of blanks. Now count the number of each letter in the word you want to check for a match. For each letter that is NOT in the original letter set, decrement the number of blanks by 1. If the number of blanks goes below 0, the word can not be formed by your letter set.
A very simple linear matching algorithm. Something like the following:
use strict;
use warnings;
my $letters = 'balphe_';
my (%lhash, @solutions, $blanks, $bcopy, $l);
$blanks++ while $letters =~ s/[^a-z]//;
$lhash{$_}++ for split //, $letters;
while (<DATA>) {
$bcopy = $blanks;
my %whash; chomp; $whash{$_}++ for split //;
for $l (keys %whash) {
no warnings;
last if $lhash{$l} < $whash{$l} &&
($bcopy -= $whash{$l} - $lhash{$l}) < 0;
}
push @solutions, $_ if $bcopy > -1;
}
print join "\n",
sort {length($b) <=> length($a) || $a cmp $b}
@solutions;
__DATA__
alpha
beta
gamma
delta
epsilon
I sort of slapped this together on the spot, so don't expect it to be pretty. It should work, however.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.