Greatest common substringShortest Longest Common Subsequence CodeDecompose a StringGiven a string find the substring which appears most oftenFind Patterns in StringsShortest Longest Common Subsequence CodeMaximal Substring ConstructionVisualize the greatest common divisorShortest Unique SubstringAs Easy As A-B-CFind the original string, without the repetition without the repetition in the middleThe Third String

Is there a conventional notation or name for the slip angle?

Create all possible words using a set or letters

Does the Mind Blank spell prevent the target from being frightened?

Greatest common substring

Find last 3 digits of this monster number

On a tidally locked planet, would time be quantized?

List of people who lose a child in תנ"ך

Why does Async/Await work properly when the loop is inside the async function and not the other way around?

When quoting, must I also copy hyphens used to divide words that continue on the next line?

Is possible to search in vim history?

How can Trident be so inexpensive? Will it orbit Triton or just do a (slow) flyby?

How should I respond when I lied about my education and the company finds out through background check?

Why is Arduino resetting while driving motors?

Proving a function is onto where f(x)=|x|.

Melting point of aspirin, contradicting sources

Translation of Scottish 16th century church stained glass

Flux received by a negative charge

Reply 'no position' while the job posting is still there

ArcGIS not connecting to PostgreSQL db with all upper-case name

THT: What is a squared annular “ring”?

Why has "pence" been used in this sentence, not "pences"?

Is it possible to use .desktop files to open local pdf files on specific pages with a browser?

Did US corporations pay demonstrators in the German demonstrations against article 13?

Did arcade monitors have same pixel aspect ratio as TV sets?



Greatest common substring


Shortest Longest Common Subsequence CodeDecompose a StringGiven a string find the substring which appears most oftenFind Patterns in StringsShortest Longest Common Subsequence CodeMaximal Substring ConstructionVisualize the greatest common divisorShortest Unique SubstringAs Easy As A-B-CFind the original string, without the repetition without the repetition in the middleThe Third String













5












$begingroup$


Create a program or function which takes a list of strings as input, and outputs the longest string that is a substring of all input strings. If there are several substrings of equal length, and no longer substring, output any one of them.



  • This may mean outputting the empty string.

  • If there are several valid outputs, you may output any one of them. You are not required to give consistent outpput for a given input so long as the output is always valid.

  • There will always be at least one string in the input, but there might not be a non-empty string.

  • All printable ASCII characters may appear in the input. You may assume those are the only characters that appear.

  • You may take input or produce output by any of the default methods.


  • Standard loopholes aren't allowed.

  • This is code-golf - the fewer bytes of code, the better.

Test cases:



[Inputs] -> [Valid outputs (choose one)]



["hello", "'ello"] -> ["ello"]
["very", "much", "different"] -> [""]
["empty", "", "STRING"] -> [""]
["identical", "identical"] -> ["identical"]
["string", "stRIng"] -> ["st", "ng"]
["this one", "is a substring of this one"] -> ["this one"]
["just one"] -> ["just one"]
["", "", ""] -> [""]
["many outputs", "stuptuo ynam"] -> ["m", "a", "n", "y", " ", "o", "u", "t", "p", "s"]
["many inputs", "any inputs", "ny iii", "yanny"] -> ["ny"]
["%%not&", "ju&#st", "[&]alpha_numeric"] -> ["%", "&"]









share|improve this question











$endgroup$











  • $begingroup$
    Possible duplicate
    $endgroup$
    – Adám
    2 hours ago










  • $begingroup$
    @Adám That question asks for the longest common subsequence, not substring.
    $endgroup$
    – Doorknob
    2 hours ago






  • 1




    $begingroup$
    Will the strings be only alphanumeric, or alphabetic, or only printable-ascii?
    $endgroup$
    – Embodiment of Ignorance
    1 hour ago










  • $begingroup$
    @EmbodimentofIgnorance All printable ASCII characters can appear in the input.
    $endgroup$
    – Sara J
    1 hour ago










  • $begingroup$
    @JoKing Yeah, it can. Apparently I'm too tired for this.
    $endgroup$
    – Sara J
    17 mins ago
















5












$begingroup$


Create a program or function which takes a list of strings as input, and outputs the longest string that is a substring of all input strings. If there are several substrings of equal length, and no longer substring, output any one of them.



  • This may mean outputting the empty string.

  • If there are several valid outputs, you may output any one of them. You are not required to give consistent outpput for a given input so long as the output is always valid.

  • There will always be at least one string in the input, but there might not be a non-empty string.

  • All printable ASCII characters may appear in the input. You may assume those are the only characters that appear.

  • You may take input or produce output by any of the default methods.


  • Standard loopholes aren't allowed.

  • This is code-golf - the fewer bytes of code, the better.

Test cases:



[Inputs] -> [Valid outputs (choose one)]



["hello", "'ello"] -> ["ello"]
["very", "much", "different"] -> [""]
["empty", "", "STRING"] -> [""]
["identical", "identical"] -> ["identical"]
["string", "stRIng"] -> ["st", "ng"]
["this one", "is a substring of this one"] -> ["this one"]
["just one"] -> ["just one"]
["", "", ""] -> [""]
["many outputs", "stuptuo ynam"] -> ["m", "a", "n", "y", " ", "o", "u", "t", "p", "s"]
["many inputs", "any inputs", "ny iii", "yanny"] -> ["ny"]
["%%not&", "ju&#st", "[&]alpha_numeric"] -> ["%", "&"]









share|improve this question











$endgroup$











  • $begingroup$
    Possible duplicate
    $endgroup$
    – Adám
    2 hours ago










  • $begingroup$
    @Adám That question asks for the longest common subsequence, not substring.
    $endgroup$
    – Doorknob
    2 hours ago






  • 1




    $begingroup$
    Will the strings be only alphanumeric, or alphabetic, or only printable-ascii?
    $endgroup$
    – Embodiment of Ignorance
    1 hour ago










  • $begingroup$
    @EmbodimentofIgnorance All printable ASCII characters can appear in the input.
    $endgroup$
    – Sara J
    1 hour ago










  • $begingroup$
    @JoKing Yeah, it can. Apparently I'm too tired for this.
    $endgroup$
    – Sara J
    17 mins ago














5












5








5





$begingroup$


Create a program or function which takes a list of strings as input, and outputs the longest string that is a substring of all input strings. If there are several substrings of equal length, and no longer substring, output any one of them.



  • This may mean outputting the empty string.

  • If there are several valid outputs, you may output any one of them. You are not required to give consistent outpput for a given input so long as the output is always valid.

  • There will always be at least one string in the input, but there might not be a non-empty string.

  • All printable ASCII characters may appear in the input. You may assume those are the only characters that appear.

  • You may take input or produce output by any of the default methods.


  • Standard loopholes aren't allowed.

  • This is code-golf - the fewer bytes of code, the better.

Test cases:



[Inputs] -> [Valid outputs (choose one)]



["hello", "'ello"] -> ["ello"]
["very", "much", "different"] -> [""]
["empty", "", "STRING"] -> [""]
["identical", "identical"] -> ["identical"]
["string", "stRIng"] -> ["st", "ng"]
["this one", "is a substring of this one"] -> ["this one"]
["just one"] -> ["just one"]
["", "", ""] -> [""]
["many outputs", "stuptuo ynam"] -> ["m", "a", "n", "y", " ", "o", "u", "t", "p", "s"]
["many inputs", "any inputs", "ny iii", "yanny"] -> ["ny"]
["%%not&", "ju&#st", "[&]alpha_numeric"] -> ["%", "&"]









share|improve this question











$endgroup$




Create a program or function which takes a list of strings as input, and outputs the longest string that is a substring of all input strings. If there are several substrings of equal length, and no longer substring, output any one of them.



  • This may mean outputting the empty string.

  • If there are several valid outputs, you may output any one of them. You are not required to give consistent outpput for a given input so long as the output is always valid.

  • There will always be at least one string in the input, but there might not be a non-empty string.

  • All printable ASCII characters may appear in the input. You may assume those are the only characters that appear.

  • You may take input or produce output by any of the default methods.


  • Standard loopholes aren't allowed.

  • This is code-golf - the fewer bytes of code, the better.

Test cases:



[Inputs] -> [Valid outputs (choose one)]



["hello", "'ello"] -> ["ello"]
["very", "much", "different"] -> [""]
["empty", "", "STRING"] -> [""]
["identical", "identical"] -> ["identical"]
["string", "stRIng"] -> ["st", "ng"]
["this one", "is a substring of this one"] -> ["this one"]
["just one"] -> ["just one"]
["", "", ""] -> [""]
["many outputs", "stuptuo ynam"] -> ["m", "a", "n", "y", " ", "o", "u", "t", "p", "s"]
["many inputs", "any inputs", "ny iii", "yanny"] -> ["ny"]
["%%not&", "ju&#st", "[&]alpha_numeric"] -> ["%", "&"]






code-golf string subsequence






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 18 mins ago







Sara J

















asked 3 hours ago









Sara JSara J

1765




1765











  • $begingroup$
    Possible duplicate
    $endgroup$
    – Adám
    2 hours ago










  • $begingroup$
    @Adám That question asks for the longest common subsequence, not substring.
    $endgroup$
    – Doorknob
    2 hours ago






  • 1




    $begingroup$
    Will the strings be only alphanumeric, or alphabetic, or only printable-ascii?
    $endgroup$
    – Embodiment of Ignorance
    1 hour ago










  • $begingroup$
    @EmbodimentofIgnorance All printable ASCII characters can appear in the input.
    $endgroup$
    – Sara J
    1 hour ago










  • $begingroup$
    @JoKing Yeah, it can. Apparently I'm too tired for this.
    $endgroup$
    – Sara J
    17 mins ago

















  • $begingroup$
    Possible duplicate
    $endgroup$
    – Adám
    2 hours ago










  • $begingroup$
    @Adám That question asks for the longest common subsequence, not substring.
    $endgroup$
    – Doorknob
    2 hours ago






  • 1




    $begingroup$
    Will the strings be only alphanumeric, or alphabetic, or only printable-ascii?
    $endgroup$
    – Embodiment of Ignorance
    1 hour ago










  • $begingroup$
    @EmbodimentofIgnorance All printable ASCII characters can appear in the input.
    $endgroup$
    – Sara J
    1 hour ago










  • $begingroup$
    @JoKing Yeah, it can. Apparently I'm too tired for this.
    $endgroup$
    – Sara J
    17 mins ago
















$begingroup$
Possible duplicate
$endgroup$
– Adám
2 hours ago




$begingroup$
Possible duplicate
$endgroup$
– Adám
2 hours ago












$begingroup$
@Adám That question asks for the longest common subsequence, not substring.
$endgroup$
– Doorknob
2 hours ago




$begingroup$
@Adám That question asks for the longest common subsequence, not substring.
$endgroup$
– Doorknob
2 hours ago




1




1




$begingroup$
Will the strings be only alphanumeric, or alphabetic, or only printable-ascii?
$endgroup$
– Embodiment of Ignorance
1 hour ago




$begingroup$
Will the strings be only alphanumeric, or alphabetic, or only printable-ascii?
$endgroup$
– Embodiment of Ignorance
1 hour ago












$begingroup$
@EmbodimentofIgnorance All printable ASCII characters can appear in the input.
$endgroup$
– Sara J
1 hour ago




$begingroup$
@EmbodimentofIgnorance All printable ASCII characters can appear in the input.
$endgroup$
– Sara J
1 hour ago












$begingroup$
@JoKing Yeah, it can. Apparently I'm too tired for this.
$endgroup$
– Sara J
17 mins ago





$begingroup$
@JoKing Yeah, it can. Apparently I'm too tired for this.
$endgroup$
– Sara J
17 mins ago











8 Answers
8






active

oldest

votes


















1












$begingroup$


Python 3, 137 bytes





def a(b):c=[[d[f:e]for e in range(len(d)+1)for f in range(e+1)]for d in b];return max([i for i in c[0]if all(i in j for j in c)],key=len)


Try it online!






share|improve this answer










New contributor




Artemis Fowl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$












  • $begingroup$
    You may want to use single spaces as indentation instead of 4 that seems to shave more than 100 bytes.
    $endgroup$
    – Shieru Asakoto
    2 hours ago










  • $begingroup$
    @ShieruAsakoto Oops yeah.
    $endgroup$
    – Artemis Fowl
    2 hours ago










  • $begingroup$
    @JoKing tio.run/…
    $endgroup$
    – Artemis Fowl
    1 hour ago










  • $begingroup$
    @JoKing The one currently in my answer is fine for all the test cases.
    $endgroup$
    – Artemis Fowl
    1 hour ago










  • $begingroup$
    Right, here's a fixed version of the 135 byte program
    $endgroup$
    – Jo King
    1 hour ago



















0












$begingroup$


Jelly, 12 bytes



Ẇ€œ&/LÐṀḢ¹L?


Try it online!



Last four bytes are there because of the requirement to only output one answer.






share|improve this answer









$endgroup$




















    0












    $begingroup$


    Brachylog (v2), 3 bytes



    sᵛw


    Try it online!



    Full program. Input from standard input (as a JSON-style list of strings), output to standard output.



    Explanation



    sᵛw
    s Find a substring
    ᵛ of every element of the input; the same one for each
    w and output it.


    Tiebreak order here is set by the s, favouring the longest substring (the secondary tiebreak doesn't matter, but IIRC it's position within the first element of the input).



    Brachylog's s doesn't return empty substrings, so we need a bit of a trick to get around that: instead of making a function submission (which is what's normally done), we write a full program, outputting to standard output. That way, if there's a common substring, we just output it, and we're done. If there isn't a common substring, the program errors out – but it still prints nothing to standard output, thus it outputs the null string as intended.






    share|improve this answer











    $endgroup$




















      0












      $begingroup$


      Perl 6, 62 bytes





      ~sort(-*.comb,keys [∩] .map(*.comb[^*X.. ^*+1]>>.join))[0]


      Try it online!



      I'm a little annoyed the Perl 6 can't do set operations on lists of lists, which is why there's an extra .comb and >> in there. Another annoying thing is that max can't take an function for how to compare items, meaning I have to user sort instead.






      share|improve this answer









      $endgroup$




















        0












        $begingroup$


        Japt v2.0a0, 24 bytes



        mã c f@eøXÃr@XÊ>YÊ?X:Y}P


        This is a mess. Why isn't there a max function for arrays in Japt?



        Try it online!






        share|improve this answer









        $endgroup$




















          0












          $begingroup$


          JavaScript (Node.js), 106 bytes



          This assumes presence of regex special characters.





          a=>(F=(l,n,w=a[0].substr(n,l))=>l?n<0?F(--l,L-l):a.some(y=>y.indexOf(w)<0)?F(l,n-1):w:"")(L=a[0].length,0)


          Try it online!




          JavaScript (Node.js), 114 105 bytes



          This assumes absence of regex special characters.





          a=>(F=(l,n,w=a[0].substr(n,l))=>l?n<0?F(--l,L-l):a.some(y=>y.search(w)<0)?F(l,n-1):w:"")(L=a[0].length,0)


          Try it online!



          Probably still golfable.






          share|improve this answer











          $endgroup$




















            0












            $begingroup$


            Zsh, 126 bytes





            for l in $@
            a=
            for i in 0..$[$#l**2]
            a+=($l[1+i/$#l,1+i%$#l])

            b=($$b-$a:*a)

            for i in $b
            (($#x<$#i))&&x=$i

            <<<$x


            Try it online!



            We read all possible substrings into $a, and then intersect the common elements of $a and $b. The construct $b-$a will only substitue $a on the first iteration: Even if $b is empty (no common strings), it is still set.



            for l in $@; 
            a= # empty a
            for i in 0..$[$#l**2]; # compound double loop using div/mod
            a+=( $l[1+i/$#l,1+i%$#l] ) # append to a all possible substrings of the given line

            b=( $$b-$a:*a )
            # $b-$a # if b is unset substitute $a
            # $ :*a # take common elements of $b-$a and $a
            # b=( ) # set b to those elements

            for i in $b; # for every common substring
            (( $#x < $#i ))&&x=$i # if the current word is longer, use it

            <<<$x # print to stdout





            share|improve this answer










            New contributor




            GammaFunction is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            $endgroup$




















              0












              $begingroup$


              Python 2, 103 bytes





              lambda b:max(reduce(set.__and__,[d[f:e]for e in range(len(d)+2)for f in range(e)for d in b]),key=len)


              Try it online!



              This is an anonymous lambda that transforms each element into the set of all substrings, then reduces it by set intersection (set.__and__) and then returns the max element by length.






              share|improve this answer











              $endgroup$












                Your Answer





                StackExchange.ifUsing("editor", function ()
                return StackExchange.using("mathjaxEditing", function ()
                StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
                StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
                );
                );
                , "mathjax-editing");

                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "200"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                draft saved

                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f182134%2fgreatest-common-substring%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                8 Answers
                8






                active

                oldest

                votes








                8 Answers
                8






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1












                $begingroup$


                Python 3, 137 bytes





                def a(b):c=[[d[f:e]for e in range(len(d)+1)for f in range(e+1)]for d in b];return max([i for i in c[0]if all(i in j for j in c)],key=len)


                Try it online!






                share|improve this answer










                New contributor




                Artemis Fowl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                $endgroup$












                • $begingroup$
                  You may want to use single spaces as indentation instead of 4 that seems to shave more than 100 bytes.
                  $endgroup$
                  – Shieru Asakoto
                  2 hours ago










                • $begingroup$
                  @ShieruAsakoto Oops yeah.
                  $endgroup$
                  – Artemis Fowl
                  2 hours ago










                • $begingroup$
                  @JoKing tio.run/…
                  $endgroup$
                  – Artemis Fowl
                  1 hour ago










                • $begingroup$
                  @JoKing The one currently in my answer is fine for all the test cases.
                  $endgroup$
                  – Artemis Fowl
                  1 hour ago










                • $begingroup$
                  Right, here's a fixed version of the 135 byte program
                  $endgroup$
                  – Jo King
                  1 hour ago
















                1












                $begingroup$


                Python 3, 137 bytes





                def a(b):c=[[d[f:e]for e in range(len(d)+1)for f in range(e+1)]for d in b];return max([i for i in c[0]if all(i in j for j in c)],key=len)


                Try it online!






                share|improve this answer










                New contributor




                Artemis Fowl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                $endgroup$












                • $begingroup$
                  You may want to use single spaces as indentation instead of 4 that seems to shave more than 100 bytes.
                  $endgroup$
                  – Shieru Asakoto
                  2 hours ago










                • $begingroup$
                  @ShieruAsakoto Oops yeah.
                  $endgroup$
                  – Artemis Fowl
                  2 hours ago










                • $begingroup$
                  @JoKing tio.run/…
                  $endgroup$
                  – Artemis Fowl
                  1 hour ago










                • $begingroup$
                  @JoKing The one currently in my answer is fine for all the test cases.
                  $endgroup$
                  – Artemis Fowl
                  1 hour ago










                • $begingroup$
                  Right, here's a fixed version of the 135 byte program
                  $endgroup$
                  – Jo King
                  1 hour ago














                1












                1








                1





                $begingroup$


                Python 3, 137 bytes





                def a(b):c=[[d[f:e]for e in range(len(d)+1)for f in range(e+1)]for d in b];return max([i for i in c[0]if all(i in j for j in c)],key=len)


                Try it online!






                share|improve this answer










                New contributor




                Artemis Fowl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                $endgroup$




                Python 3, 137 bytes





                def a(b):c=[[d[f:e]for e in range(len(d)+1)for f in range(e+1)]for d in b];return max([i for i in c[0]if all(i in j for j in c)],key=len)


                Try it online!







                share|improve this answer










                New contributor




                Artemis Fowl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                share|improve this answer



                share|improve this answer








                edited 1 hour ago





















                New contributor




                Artemis Fowl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                answered 2 hours ago









                Artemis FowlArtemis Fowl

                1114




                1114




                New contributor




                Artemis Fowl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





                New contributor





                Artemis Fowl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                Artemis Fowl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.











                • $begingroup$
                  You may want to use single spaces as indentation instead of 4 that seems to shave more than 100 bytes.
                  $endgroup$
                  – Shieru Asakoto
                  2 hours ago










                • $begingroup$
                  @ShieruAsakoto Oops yeah.
                  $endgroup$
                  – Artemis Fowl
                  2 hours ago










                • $begingroup$
                  @JoKing tio.run/…
                  $endgroup$
                  – Artemis Fowl
                  1 hour ago










                • $begingroup$
                  @JoKing The one currently in my answer is fine for all the test cases.
                  $endgroup$
                  – Artemis Fowl
                  1 hour ago










                • $begingroup$
                  Right, here's a fixed version of the 135 byte program
                  $endgroup$
                  – Jo King
                  1 hour ago

















                • $begingroup$
                  You may want to use single spaces as indentation instead of 4 that seems to shave more than 100 bytes.
                  $endgroup$
                  – Shieru Asakoto
                  2 hours ago










                • $begingroup$
                  @ShieruAsakoto Oops yeah.
                  $endgroup$
                  – Artemis Fowl
                  2 hours ago










                • $begingroup$
                  @JoKing tio.run/…
                  $endgroup$
                  – Artemis Fowl
                  1 hour ago










                • $begingroup$
                  @JoKing The one currently in my answer is fine for all the test cases.
                  $endgroup$
                  – Artemis Fowl
                  1 hour ago










                • $begingroup$
                  Right, here's a fixed version of the 135 byte program
                  $endgroup$
                  – Jo King
                  1 hour ago
















                $begingroup$
                You may want to use single spaces as indentation instead of 4 that seems to shave more than 100 bytes.
                $endgroup$
                – Shieru Asakoto
                2 hours ago




                $begingroup$
                You may want to use single spaces as indentation instead of 4 that seems to shave more than 100 bytes.
                $endgroup$
                – Shieru Asakoto
                2 hours ago












                $begingroup$
                @ShieruAsakoto Oops yeah.
                $endgroup$
                – Artemis Fowl
                2 hours ago




                $begingroup$
                @ShieruAsakoto Oops yeah.
                $endgroup$
                – Artemis Fowl
                2 hours ago












                $begingroup$
                @JoKing tio.run/…
                $endgroup$
                – Artemis Fowl
                1 hour ago




                $begingroup$
                @JoKing tio.run/…
                $endgroup$
                – Artemis Fowl
                1 hour ago












                $begingroup$
                @JoKing The one currently in my answer is fine for all the test cases.
                $endgroup$
                – Artemis Fowl
                1 hour ago




                $begingroup$
                @JoKing The one currently in my answer is fine for all the test cases.
                $endgroup$
                – Artemis Fowl
                1 hour ago












                $begingroup$
                Right, here's a fixed version of the 135 byte program
                $endgroup$
                – Jo King
                1 hour ago





                $begingroup$
                Right, here's a fixed version of the 135 byte program
                $endgroup$
                – Jo King
                1 hour ago












                0












                $begingroup$


                Jelly, 12 bytes



                Ẇ€œ&/LÐṀḢ¹L?


                Try it online!



                Last four bytes are there because of the requirement to only output one answer.






                share|improve this answer









                $endgroup$

















                  0












                  $begingroup$


                  Jelly, 12 bytes



                  Ẇ€œ&/LÐṀḢ¹L?


                  Try it online!



                  Last four bytes are there because of the requirement to only output one answer.






                  share|improve this answer









                  $endgroup$















                    0












                    0








                    0





                    $begingroup$


                    Jelly, 12 bytes



                    Ẇ€œ&/LÐṀḢ¹L?


                    Try it online!



                    Last four bytes are there because of the requirement to only output one answer.






                    share|improve this answer









                    $endgroup$




                    Jelly, 12 bytes



                    Ẇ€œ&/LÐṀḢ¹L?


                    Try it online!



                    Last four bytes are there because of the requirement to only output one answer.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 2 hours ago









                    Nick KennedyNick Kennedy

                    92147




                    92147





















                        0












                        $begingroup$


                        Brachylog (v2), 3 bytes



                        sᵛw


                        Try it online!



                        Full program. Input from standard input (as a JSON-style list of strings), output to standard output.



                        Explanation



                        sᵛw
                        s Find a substring
                        ᵛ of every element of the input; the same one for each
                        w and output it.


                        Tiebreak order here is set by the s, favouring the longest substring (the secondary tiebreak doesn't matter, but IIRC it's position within the first element of the input).



                        Brachylog's s doesn't return empty substrings, so we need a bit of a trick to get around that: instead of making a function submission (which is what's normally done), we write a full program, outputting to standard output. That way, if there's a common substring, we just output it, and we're done. If there isn't a common substring, the program errors out – but it still prints nothing to standard output, thus it outputs the null string as intended.






                        share|improve this answer











                        $endgroup$

















                          0












                          $begingroup$


                          Brachylog (v2), 3 bytes



                          sᵛw


                          Try it online!



                          Full program. Input from standard input (as a JSON-style list of strings), output to standard output.



                          Explanation



                          sᵛw
                          s Find a substring
                          ᵛ of every element of the input; the same one for each
                          w and output it.


                          Tiebreak order here is set by the s, favouring the longest substring (the secondary tiebreak doesn't matter, but IIRC it's position within the first element of the input).



                          Brachylog's s doesn't return empty substrings, so we need a bit of a trick to get around that: instead of making a function submission (which is what's normally done), we write a full program, outputting to standard output. That way, if there's a common substring, we just output it, and we're done. If there isn't a common substring, the program errors out – but it still prints nothing to standard output, thus it outputs the null string as intended.






                          share|improve this answer











                          $endgroup$















                            0












                            0








                            0





                            $begingroup$


                            Brachylog (v2), 3 bytes



                            sᵛw


                            Try it online!



                            Full program. Input from standard input (as a JSON-style list of strings), output to standard output.



                            Explanation



                            sᵛw
                            s Find a substring
                            ᵛ of every element of the input; the same one for each
                            w and output it.


                            Tiebreak order here is set by the s, favouring the longest substring (the secondary tiebreak doesn't matter, but IIRC it's position within the first element of the input).



                            Brachylog's s doesn't return empty substrings, so we need a bit of a trick to get around that: instead of making a function submission (which is what's normally done), we write a full program, outputting to standard output. That way, if there's a common substring, we just output it, and we're done. If there isn't a common substring, the program errors out – but it still prints nothing to standard output, thus it outputs the null string as intended.






                            share|improve this answer











                            $endgroup$




                            Brachylog (v2), 3 bytes



                            sᵛw


                            Try it online!



                            Full program. Input from standard input (as a JSON-style list of strings), output to standard output.



                            Explanation



                            sᵛw
                            s Find a substring
                            ᵛ of every element of the input; the same one for each
                            w and output it.


                            Tiebreak order here is set by the s, favouring the longest substring (the secondary tiebreak doesn't matter, but IIRC it's position within the first element of the input).



                            Brachylog's s doesn't return empty substrings, so we need a bit of a trick to get around that: instead of making a function submission (which is what's normally done), we write a full program, outputting to standard output. That way, if there's a common substring, we just output it, and we're done. If there isn't a common substring, the program errors out – but it still prints nothing to standard output, thus it outputs the null string as intended.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            answered 1 hour ago


























                            community wiki





                            ais523






















                                0












                                $begingroup$


                                Perl 6, 62 bytes





                                ~sort(-*.comb,keys [∩] .map(*.comb[^*X.. ^*+1]>>.join))[0]


                                Try it online!



                                I'm a little annoyed the Perl 6 can't do set operations on lists of lists, which is why there's an extra .comb and >> in there. Another annoying thing is that max can't take an function for how to compare items, meaning I have to user sort instead.






                                share|improve this answer









                                $endgroup$

















                                  0












                                  $begingroup$


                                  Perl 6, 62 bytes





                                  ~sort(-*.comb,keys [∩] .map(*.comb[^*X.. ^*+1]>>.join))[0]


                                  Try it online!



                                  I'm a little annoyed the Perl 6 can't do set operations on lists of lists, which is why there's an extra .comb and >> in there. Another annoying thing is that max can't take an function for how to compare items, meaning I have to user sort instead.






                                  share|improve this answer









                                  $endgroup$















                                    0












                                    0








                                    0





                                    $begingroup$


                                    Perl 6, 62 bytes





                                    ~sort(-*.comb,keys [∩] .map(*.comb[^*X.. ^*+1]>>.join))[0]


                                    Try it online!



                                    I'm a little annoyed the Perl 6 can't do set operations on lists of lists, which is why there's an extra .comb and >> in there. Another annoying thing is that max can't take an function for how to compare items, meaning I have to user sort instead.






                                    share|improve this answer









                                    $endgroup$




                                    Perl 6, 62 bytes





                                    ~sort(-*.comb,keys [∩] .map(*.comb[^*X.. ^*+1]>>.join))[0]


                                    Try it online!



                                    I'm a little annoyed the Perl 6 can't do set operations on lists of lists, which is why there's an extra .comb and >> in there. Another annoying thing is that max can't take an function for how to compare items, meaning I have to user sort instead.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered 54 mins ago









                                    Jo KingJo King

                                    25.3k361129




                                    25.3k361129





















                                        0












                                        $begingroup$


                                        Japt v2.0a0, 24 bytes



                                        mã c f@eøXÃr@XÊ>YÊ?X:Y}P


                                        This is a mess. Why isn't there a max function for arrays in Japt?



                                        Try it online!






                                        share|improve this answer









                                        $endgroup$

















                                          0












                                          $begingroup$


                                          Japt v2.0a0, 24 bytes



                                          mã c f@eøXÃr@XÊ>YÊ?X:Y}P


                                          This is a mess. Why isn't there a max function for arrays in Japt?



                                          Try it online!






                                          share|improve this answer









                                          $endgroup$















                                            0












                                            0








                                            0





                                            $begingroup$


                                            Japt v2.0a0, 24 bytes



                                            mã c f@eøXÃr@XÊ>YÊ?X:Y}P


                                            This is a mess. Why isn't there a max function for arrays in Japt?



                                            Try it online!






                                            share|improve this answer









                                            $endgroup$




                                            Japt v2.0a0, 24 bytes



                                            mã c f@eøXÃr@XÊ>YÊ?X:Y}P


                                            This is a mess. Why isn't there a max function for arrays in Japt?



                                            Try it online!







                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered 52 mins ago









                                            Embodiment of IgnoranceEmbodiment of Ignorance

                                            2,138125




                                            2,138125





















                                                0












                                                $begingroup$


                                                JavaScript (Node.js), 106 bytes



                                                This assumes presence of regex special characters.





                                                a=>(F=(l,n,w=a[0].substr(n,l))=>l?n<0?F(--l,L-l):a.some(y=>y.indexOf(w)<0)?F(l,n-1):w:"")(L=a[0].length,0)


                                                Try it online!




                                                JavaScript (Node.js), 114 105 bytes



                                                This assumes absence of regex special characters.





                                                a=>(F=(l,n,w=a[0].substr(n,l))=>l?n<0?F(--l,L-l):a.some(y=>y.search(w)<0)?F(l,n-1):w:"")(L=a[0].length,0)


                                                Try it online!



                                                Probably still golfable.






                                                share|improve this answer











                                                $endgroup$

















                                                  0












                                                  $begingroup$


                                                  JavaScript (Node.js), 106 bytes



                                                  This assumes presence of regex special characters.





                                                  a=>(F=(l,n,w=a[0].substr(n,l))=>l?n<0?F(--l,L-l):a.some(y=>y.indexOf(w)<0)?F(l,n-1):w:"")(L=a[0].length,0)


                                                  Try it online!




                                                  JavaScript (Node.js), 114 105 bytes



                                                  This assumes absence of regex special characters.





                                                  a=>(F=(l,n,w=a[0].substr(n,l))=>l?n<0?F(--l,L-l):a.some(y=>y.search(w)<0)?F(l,n-1):w:"")(L=a[0].length,0)


                                                  Try it online!



                                                  Probably still golfable.






                                                  share|improve this answer











                                                  $endgroup$















                                                    0












                                                    0








                                                    0





                                                    $begingroup$


                                                    JavaScript (Node.js), 106 bytes



                                                    This assumes presence of regex special characters.





                                                    a=>(F=(l,n,w=a[0].substr(n,l))=>l?n<0?F(--l,L-l):a.some(y=>y.indexOf(w)<0)?F(l,n-1):w:"")(L=a[0].length,0)


                                                    Try it online!




                                                    JavaScript (Node.js), 114 105 bytes



                                                    This assumes absence of regex special characters.





                                                    a=>(F=(l,n,w=a[0].substr(n,l))=>l?n<0?F(--l,L-l):a.some(y=>y.search(w)<0)?F(l,n-1):w:"")(L=a[0].length,0)


                                                    Try it online!



                                                    Probably still golfable.






                                                    share|improve this answer











                                                    $endgroup$




                                                    JavaScript (Node.js), 106 bytes



                                                    This assumes presence of regex special characters.





                                                    a=>(F=(l,n,w=a[0].substr(n,l))=>l?n<0?F(--l,L-l):a.some(y=>y.indexOf(w)<0)?F(l,n-1):w:"")(L=a[0].length,0)


                                                    Try it online!




                                                    JavaScript (Node.js), 114 105 bytes



                                                    This assumes absence of regex special characters.





                                                    a=>(F=(l,n,w=a[0].substr(n,l))=>l?n<0?F(--l,L-l):a.some(y=>y.search(w)<0)?F(l,n-1):w:"")(L=a[0].length,0)


                                                    Try it online!



                                                    Probably still golfable.







                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited 43 mins ago

























                                                    answered 1 hour ago









                                                    Shieru AsakotoShieru Asakoto

                                                    2,750317




                                                    2,750317





















                                                        0












                                                        $begingroup$


                                                        Zsh, 126 bytes





                                                        for l in $@
                                                        a=
                                                        for i in 0..$[$#l**2]
                                                        a+=($l[1+i/$#l,1+i%$#l])

                                                        b=($$b-$a:*a)

                                                        for i in $b
                                                        (($#x<$#i))&&x=$i

                                                        <<<$x


                                                        Try it online!



                                                        We read all possible substrings into $a, and then intersect the common elements of $a and $b. The construct $b-$a will only substitue $a on the first iteration: Even if $b is empty (no common strings), it is still set.



                                                        for l in $@; 
                                                        a= # empty a
                                                        for i in 0..$[$#l**2]; # compound double loop using div/mod
                                                        a+=( $l[1+i/$#l,1+i%$#l] ) # append to a all possible substrings of the given line

                                                        b=( $$b-$a:*a )
                                                        # $b-$a # if b is unset substitute $a
                                                        # $ :*a # take common elements of $b-$a and $a
                                                        # b=( ) # set b to those elements

                                                        for i in $b; # for every common substring
                                                        (( $#x < $#i ))&&x=$i # if the current word is longer, use it

                                                        <<<$x # print to stdout





                                                        share|improve this answer










                                                        New contributor




                                                        GammaFunction is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                        Check out our Code of Conduct.






                                                        $endgroup$

















                                                          0












                                                          $begingroup$


                                                          Zsh, 126 bytes





                                                          for l in $@
                                                          a=
                                                          for i in 0..$[$#l**2]
                                                          a+=($l[1+i/$#l,1+i%$#l])

                                                          b=($$b-$a:*a)

                                                          for i in $b
                                                          (($#x<$#i))&&x=$i

                                                          <<<$x


                                                          Try it online!



                                                          We read all possible substrings into $a, and then intersect the common elements of $a and $b. The construct $b-$a will only substitue $a on the first iteration: Even if $b is empty (no common strings), it is still set.



                                                          for l in $@; 
                                                          a= # empty a
                                                          for i in 0..$[$#l**2]; # compound double loop using div/mod
                                                          a+=( $l[1+i/$#l,1+i%$#l] ) # append to a all possible substrings of the given line

                                                          b=( $$b-$a:*a )
                                                          # $b-$a # if b is unset substitute $a
                                                          # $ :*a # take common elements of $b-$a and $a
                                                          # b=( ) # set b to those elements

                                                          for i in $b; # for every common substring
                                                          (( $#x < $#i ))&&x=$i # if the current word is longer, use it

                                                          <<<$x # print to stdout





                                                          share|improve this answer










                                                          New contributor




                                                          GammaFunction is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                          Check out our Code of Conduct.






                                                          $endgroup$















                                                            0












                                                            0








                                                            0





                                                            $begingroup$


                                                            Zsh, 126 bytes





                                                            for l in $@
                                                            a=
                                                            for i in 0..$[$#l**2]
                                                            a+=($l[1+i/$#l,1+i%$#l])

                                                            b=($$b-$a:*a)

                                                            for i in $b
                                                            (($#x<$#i))&&x=$i

                                                            <<<$x


                                                            Try it online!



                                                            We read all possible substrings into $a, and then intersect the common elements of $a and $b. The construct $b-$a will only substitue $a on the first iteration: Even if $b is empty (no common strings), it is still set.



                                                            for l in $@; 
                                                            a= # empty a
                                                            for i in 0..$[$#l**2]; # compound double loop using div/mod
                                                            a+=( $l[1+i/$#l,1+i%$#l] ) # append to a all possible substrings of the given line

                                                            b=( $$b-$a:*a )
                                                            # $b-$a # if b is unset substitute $a
                                                            # $ :*a # take common elements of $b-$a and $a
                                                            # b=( ) # set b to those elements

                                                            for i in $b; # for every common substring
                                                            (( $#x < $#i ))&&x=$i # if the current word is longer, use it

                                                            <<<$x # print to stdout





                                                            share|improve this answer










                                                            New contributor




                                                            GammaFunction is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                            Check out our Code of Conduct.






                                                            $endgroup$




                                                            Zsh, 126 bytes





                                                            for l in $@
                                                            a=
                                                            for i in 0..$[$#l**2]
                                                            a+=($l[1+i/$#l,1+i%$#l])

                                                            b=($$b-$a:*a)

                                                            for i in $b
                                                            (($#x<$#i))&&x=$i

                                                            <<<$x


                                                            Try it online!



                                                            We read all possible substrings into $a, and then intersect the common elements of $a and $b. The construct $b-$a will only substitue $a on the first iteration: Even if $b is empty (no common strings), it is still set.



                                                            for l in $@; 
                                                            a= # empty a
                                                            for i in 0..$[$#l**2]; # compound double loop using div/mod
                                                            a+=( $l[1+i/$#l,1+i%$#l] ) # append to a all possible substrings of the given line

                                                            b=( $$b-$a:*a )
                                                            # $b-$a # if b is unset substitute $a
                                                            # $ :*a # take common elements of $b-$a and $a
                                                            # b=( ) # set b to those elements

                                                            for i in $b; # for every common substring
                                                            (( $#x < $#i ))&&x=$i # if the current word is longer, use it

                                                            <<<$x # print to stdout






                                                            share|improve this answer










                                                            New contributor




                                                            GammaFunction is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                            Check out our Code of Conduct.









                                                            share|improve this answer



                                                            share|improve this answer








                                                            edited 36 mins ago





















                                                            New contributor




                                                            GammaFunction is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                            Check out our Code of Conduct.









                                                            answered 45 mins ago









                                                            GammaFunctionGammaFunction

                                                            615




                                                            615




                                                            New contributor




                                                            GammaFunction is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                            Check out our Code of Conduct.





                                                            New contributor





                                                            GammaFunction is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                            Check out our Code of Conduct.






                                                            GammaFunction is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                            Check out our Code of Conduct.





















                                                                0












                                                                $begingroup$


                                                                Python 2, 103 bytes





                                                                lambda b:max(reduce(set.__and__,[d[f:e]for e in range(len(d)+2)for f in range(e)for d in b]),key=len)


                                                                Try it online!



                                                                This is an anonymous lambda that transforms each element into the set of all substrings, then reduces it by set intersection (set.__and__) and then returns the max element by length.






                                                                share|improve this answer











                                                                $endgroup$

















                                                                  0












                                                                  $begingroup$


                                                                  Python 2, 103 bytes





                                                                  lambda b:max(reduce(set.__and__,[d[f:e]for e in range(len(d)+2)for f in range(e)for d in b]),key=len)


                                                                  Try it online!



                                                                  This is an anonymous lambda that transforms each element into the set of all substrings, then reduces it by set intersection (set.__and__) and then returns the max element by length.






                                                                  share|improve this answer











                                                                  $endgroup$















                                                                    0












                                                                    0








                                                                    0





                                                                    $begingroup$


                                                                    Python 2, 103 bytes





                                                                    lambda b:max(reduce(set.__and__,[d[f:e]for e in range(len(d)+2)for f in range(e)for d in b]),key=len)


                                                                    Try it online!



                                                                    This is an anonymous lambda that transforms each element into the set of all substrings, then reduces it by set intersection (set.__and__) and then returns the max element by length.






                                                                    share|improve this answer











                                                                    $endgroup$




                                                                    Python 2, 103 bytes





                                                                    lambda b:max(reduce(set.__and__,[d[f:e]for e in range(len(d)+2)for f in range(e)for d in b]),key=len)


                                                                    Try it online!



                                                                    This is an anonymous lambda that transforms each element into the set of all substrings, then reduces it by set intersection (set.__and__) and then returns the max element by length.







                                                                    share|improve this answer














                                                                    share|improve this answer



                                                                    share|improve this answer








                                                                    edited 32 mins ago

























                                                                    answered 44 mins ago









                                                                    Jo KingJo King

                                                                    25.3k361129




                                                                    25.3k361129



























                                                                        draft saved

                                                                        draft discarded
















































                                                                        If this is an answer to a challenge…



                                                                        • …Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.


                                                                        • …Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
                                                                          Explanations of your answer make it more interesting to read and are very much encouraged.


                                                                        • …Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.


                                                                        More generally…



                                                                        • …Please make sure to answer the question and provide sufficient detail.


                                                                        • …Avoid asking for help, clarification or responding to other answers (use comments instead).




                                                                        draft saved


                                                                        draft discarded














                                                                        StackExchange.ready(
                                                                        function ()
                                                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f182134%2fgreatest-common-substring%23new-answer', 'question_page');

                                                                        );

                                                                        Post as a guest















                                                                        Required, but never shown





















































                                                                        Required, but never shown














                                                                        Required, but never shown












                                                                        Required, but never shown







                                                                        Required, but never shown

































                                                                        Required, but never shown














                                                                        Required, but never shown












                                                                        Required, but never shown







                                                                        Required, but never shown







                                                                        Popular posts from this blog

                                                                        名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

                                                                        Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

                                                                        Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar