How to resolve the algorithm Longest common substring step by step in the SETL programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Longest common substring step by step in the SETL programming language

Table of Contents

Problem Statement

Write a function that returns the longest common substring of two strings. Use it within a program that demonstrates sample output from the function, which will consist of the longest common substring between "thisisatest" and "testing123testing". Note that substrings are consecutive characters within a string.   This distinguishes them from subsequences, which is any sequence of characters within a string, even if there are extraneous characters in between them. Hence, the longest common subsequence between "thisisatest" and "testing123testing" is "tsitest", whereas the longest common substring is just "test".

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Longest common substring step by step in the SETL programming language

Source code in the setl programming language

program longest_common_substring;
    print(lcs("thisisatest", "testing123testing"));

    proc lcs(s1, s2);
        if #s1 < #s2 then [s1, s2] := [s2, s1]; end if;

        loop for l in [#s2, #s2-1..1] do
            loop for s in [1..#s2-l+1] do
                if (substr := s2(s..s+l)) in s1 then
                    return substr;
                end if;
            end loop;
        end loop;

        return "";
    end proc;
end program;

  

You may also check:How to resolve the algorithm Mertens function step by step in the REXX programming language
You may also check:How to resolve the algorithm Shell one-liner step by step in the Free Pascal programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Joy programming language
You may also check:How to resolve the algorithm Command-line arguments step by step in the min programming language
You may also check:How to resolve the algorithm Hash join step by step in the Elixir programming language