Longest common prefix: Difference between revisions

Content added Content deleted
No edit summary
No edit summary
Line 3: Line 3:


Given a set of strings, ''R'', for a prefix ''S'', it should hold that:
Given a set of strings, ''R'', for a prefix ''S'', it should hold that:
:<math>\forall x\ \in\ R:\ S \le</math><sub>pref</sub> <math>\ x</math> ~ "for all members ''x'' of set ''R'', it hold true that string ''S'' is a prefix of ''x''"
:<math>\forall x\ \in\ R:\ S \le</math><sub>pref</sub> <math>\ x</math> ~ "for all members ''x'' of set ''R'', it holds true that string ''S'' is a prefix of ''x''"
:(help here: ''does not express that ''S'' is the <u>longest</u> common prefix of ''x)
:(help here: ''does not express that ''S'' is the <u>longest</u> common prefix of ''x)
An example use case for this: given a set of phone numbers, identify a common dialing code. This can be accomplished by first determining the common prefix (if any), and then matching it against know dialing codes (iteratively dropping characters from rhs until a match is found, as the ''lcp'' function may match more than the dialing code).
An example use case for this: given a set of phone numbers, identify a common dialing code. This can be accomplished by first determining the common prefix (if any), and then matching it against know dialing codes (iteratively dropping characters from rhs until a match is found, as the ''lcp'' function may match more than the dialing code).