in reply to Re: Find common prefix from a list of strings
in thread Find common prefix from a list of strings
c:\@Work\Perl\monks\Albannach>perl -wMstrict -le "sub insert { my $trie=shift; my $str=shift; $trie=$trie->{$_}||={} foreach (split //,$str); } ;; sub common { my $trie=shift; my $common=\"\"; while (1==scalar keys %$trie) { my $char=(keys %$trie)[0]; $common.=$char; $trie=$trie->{$char}; } $common; } ;; my %trie; insert(\%trie,$_) foreach qw(a ab abc); print common(\%trie); " abc
Shouldn't 'a' be the longest common prefix?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Find common prefix from a list of strings
by demerphq (Chancellor) on Apr 11, 2016 at 18:11 UTC |