Erlang binary longest common substring

Valentin Micic v@REDACTED
Mon Mar 30 13:23:16 CEST 2020


> On 30 Mar 2020, at 12:18, I Gusti Ngurah Oka Prinarjaya <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/6171aa17/attachment.htm>


More information about the erlang-questions mailing list