Erlang binary longest common substring
Marc Worrell
marc@REDACTED
Mon Mar 30 13:41:27 CEST 2020
Maybe you would like to take UTF8 strings into account.
As copying is sometimes better, and actually quite fast, you can also build a new binary.
common_prefix(A, B) ->
common_prefix(A, B, <<>>).
common_prefix(<<C/utf8, A/binary>>, <<C/utf8, B/binary>>, Acc) ->
common_prefix(A, B, <<Acc/binary, C/utf8>>);
common_prefix(_A, _B, Acc) ->
Acc.
Or use the offset method and return a sub-binary:
common_prefix(A, B) ->
common_prefix(A, B, 0).
common_prefix(A, B, Offs) ->
case {A, B} of
{<<_:Offs/binary, C/utf8, _/binary>>, <<_:Offs/binary, C/utf8, _/binary>>} ->
common_prefix(A, B, Offs + size(<<C/utf8>>));
{<<Prefix:Offs/binary, _/binary>>, _} ->
Prefix
end.
Cheers, Marc
> On 30 Mar 2020, at 13:23, Valentin Micic <v@REDACTED> wrote:
>
>
>> On 30 Mar 2020, at 12:18, I Gusti Ngurah Oka Prinarjaya <okaprinarjaya@REDACTED <mailto:okaprinarjaya@REDACTED>> wrote:
>>
>> Hi,
>>
>> I need longest common substring erlang implementation. Please help me
>>
>> I've tried https://rosettacode.org/wiki/Longest_common_subsequence#Erlang <https://rosettacode.org/wiki/Longest_common_subsequence#Erlang> but very very slow T_T
>>
>> and i've tried binary:longest_common_prefix([<<"i love you 2020-03-28">>,<<"i love you 2020-03-01">>]) not effective / not valid
>>
>> Thank you
>>
>
> It may be faster to do this with lists, but this should do the trick:
>
> lcs( <<A/binary>>, <<B/binary>>, Off )
> ->
> case {A, B} of
> {<<_:Off/binary-unit:8, C:8, _/binary>>, <<_:Off/binary-unit:8, C:8, _/binary>>} -> lcs( A, B, Off+1 );
> {<<Common:Off/binary-unit:8, _/binary>>, _} -> Common
> end
> .
>
> Fro example, a call to lcs( <<“Hello World”>>, <<"Hellao World”>>, 0 )will return <<“Hell”>>.
>
> NOTE (and just in case): you’d need to put this function in your own module.
>
> V/
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20200330/379750c7/attachment.htm>
More information about the erlang-questions
mailing list