in reply to Longest String Matching
while (length($word) && !exists $lookup{$word}) { substr($word, 0, 1) = ""; # remove 1st char } # $word will be empty or the matched string
Update: This probably works well if the lexicon contains a lot of words as the execution time of this is proportional only to the length of the word. hdb has contributed an improvement to reduced the complexity a little, using the common trick of guaranteeing a match for zero length. There is a regex solution below this which is interesting too...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Longest String Matching
by hdb (Monsignor) on Apr 08, 2013 at 12:02 UTC | |
by space_monk (Chaplain) on Apr 08, 2013 at 12:08 UTC | |
by hdb (Monsignor) on Apr 08, 2013 at 12:17 UTC |