Longest common prefix: Difference between revisions

m
People can disagree on what "well designed" means in technical contexts, so spell out what was meant
m (People can disagree on what "well designed" means in technical contexts, so spell out what was meant)
Line 821:
<lang J>lcp=: {. {.~ 0 i.~ [: */2 =/\ ]</lang>
 
In other words: compare adjacent strings pair-wise, combine results logically, find first mismatch in any of them, take that many characters from the first of the strings. Note that we rely on J's (well designed) handling of edge cases here.
 
Note that we rely on J's handling of edge cases here. In other words: if we have only one string that falls out as the longest prefix, and if we have no strings the result is the empty string.
 
As the number of adjacent pairs is O(n) where n is the number of strings, this approach could be faster in the limit cases than sorting.
Line 841 ⟶ 843:
lcp 'prefix',:'suffix'
</lang>
 
=={{header|Java}}==
{{works with|Java|1.5+}}
6,962

edits