<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div>Maybe you would like to take UTF8 strings into account.</div><div>As copying is sometimes better, and actually quite fast, you can also build a new binary.</div><div><br class=""></div><div><font face="Courier New" class=""><br class=""></font></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div><div><div><font face="Courier New" class="">common_prefix(A, B) -></font></div><div><font face="Courier New" class=""> common_prefix(A, B, <<>>).</font></div><div><font face="Courier New" class=""><br class=""></font></div><div><font face="Courier New" class="">common_prefix(<<C/utf8, A/binary>>, <<C/utf8, B/binary>>, Acc) -></font></div><div><font face="Courier New" class=""> common_prefix(A, B, <<Acc/binary, C/utf8>>);</font></div><div><font face="Courier New" class="">common_prefix(_A, _B, Acc) -></font></div><div><font face="Courier New" class=""> Acc.</font></div><div class=""><br class=""></div></div></div></blockquote><div><div class=""><br class=""></div><div class="">Or use the offset method and return a sub-binary:</div><div class=""><br class=""></div><div class=""><br class=""></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div><div class=""><div class=""><font face="Courier New" class="">common_prefix(A, B) -></font></div></div></div><div><div class=""><div class=""><font face="Courier New" class=""> common_prefix(A, B, 0).</font></div></div></div><div><div class=""><div class=""><font face="Courier New" class=""><br class=""></font></div></div></div><div><div class=""><div class=""><font face="Courier New" class="">common_prefix(A, B, Offs) -></font></div></div></div><div><div class=""><div class=""><font face="Courier New" class=""> case {A, B} of</font></div></div></div><div><div class=""><div class=""><font face="Courier New" class=""> {<<_:Offs/binary, C/utf8, _/binary>>, <<_:Offs/binary, C/utf8, _/binary>>} -></font></div></div></div><div><div class=""><div class=""><font face="Courier New" class=""> common_prefix(A, B, Offs + size(<<C/utf8>>));</font></div></div></div><div><div class=""><div class=""><font face="Courier New" class=""> {<<Prefix:Offs/binary, _/binary>>, _} -></font></div></div></div><div><div class=""><div class=""><font face="Courier New" class=""> Prefix</font></div></div></div><div><div class=""><div class=""><font face="Courier New" class=""> end.</font></div></div></div></blockquote><div><div class=""><br class=""></div></div><div><div class=""><br class=""></div><div class="">Cheers, Marc</div><div class=""><br class=""></div></div><div><br class=""></div><div><br class=""></div><div><blockquote type="cite" class=""><div class="">On 30 Mar 2020, at 13:23, Valentin Micic <<a href="mailto:v@micic.co.za" class="">v@micic.co.za</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 30 Mar 2020, at 12:18, I Gusti Ngurah Oka Prinarjaya <<a href="mailto:okaprinarjaya@gmail.com" class="">okaprinarjaya@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hi,<div class=""><br class=""></div><div class="">I need longest common substring erlang implementation. Please help me</div><div class=""><br class=""></div><div class="">I've tried <a href="https://rosettacode.org/wiki/Longest_common_subsequence#Erlang" class="">https://rosettacode.org/wiki/Longest_common_subsequence#Erlang</a> but very very slow T_T</div><div class=""><br class=""></div><div class="">and i've tried <span style="color:rgb(0,128,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:12px;white-space:pre" class="">binary:longest_common_prefix([<<"i love you 2020-03-28">>,<<"i love you 2020-03-01">>])</span> not effective / not valid</div><div class=""><br class=""></div><div class="">Thank you </div><div class=""><br class=""></div></div>
</div></blockquote></div><br class=""><div class="">It may be faster to do this with lists, but this should do the trick:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><font face="Courier New" class="">lcs( <<A/binary>>, <<B/binary>>, Off )</font></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><font face="Courier New" class="">-></font></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><font face="Courier New" class=""> case {A, B} of</font></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><font face="Courier New" class=""> {<<_:Off/binary-unit:8, C:8, _/binary>>, <<_:Off/binary-unit:8, C:8, _/binary>>} -> lcs( A, B, Off+1 );</font></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><font face="Courier New" class=""> {<<Common:Off/binary-unit:8, _/binary>>, _}<span class="Apple-tab-span" style="white-space:pre"> </span> <span class="Apple-tab-span" style="white-space:pre"> </span> -> Common</font></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><font face="Courier New" class=""> end</font></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><font face="Courier New" class="">.</font></div><div class=""><br class=""></div><div class="">Fro example, a call to <font face="Courier New" class="">lcs( <<“Hello World”>>, <<"Hellao World”>>, 0 )</font>will return <font face="Courier New" class=""><<“Hell”>></font>.</div><div class=""><br class=""></div><div class="">NOTE (and just in case): you’d need to put this function in your own module.</div><div class=""><br class=""></div><div class="">V/</div><div class=""><br class=""></div><div class=""><br class=""></div></div></div></div></div></blockquote></div><br class=""></body></html>