in reply to Word Comparison
Update: The 'conventional' way would be to perform a count of each letter and compare counts:sub contains { # return true if $_[0] is contained in $_[1] my ($s, $t) = @_; for (split('', $s)) { return 0 unless ($t =~ s/$_//); } 1; } print contains("dog", "good"), "\n"; # -> 1 print contains("food", "fodder"), "\n"; # -> 0
While not the most efficient in this situation, this code exemplifies common types of data processing that is done with perl, so it's valuable to know about.sub contains { my ($s, $t) = @_; my (%sc, %tc); for (split('', $s)) { $sc{$_}++ }; for (split('', $t)) { $tc($_}++ }; for my $k (keys %sc) { return 0 unless $sc{$k} <= $tc{$k}; } 1; }
|
|---|