Greatest common substring

30

4

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 output 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 - 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"] -> ["&"]

Sara J

Posted 2019-03-25T00:35:03.313

Reputation: 2 576

Possible duplicate – Adám – 2019-03-25T00:51:06.523

2@Adám That question asks for the longest common subsequence, not substring. – Doorknob – 2019-03-25T00:58:04.503

1Will the strings be only alphanumeric, or alphabetic, or only printable-ascii? – Embodiment of Ignorance – 2019-03-25T01:57:13.483

@EmbodimentofIgnorance All printable ASCII characters can appear in the input. – Sara J – 2019-03-25T02:11:28.047

@JoKing Yeah, it can. Apparently I'm too tired for this. – Sara J – 2019-03-25T03:24:30.770

Can we ouput undefined instead of the empty string? – Shaggy – 2019-03-25T08:08:59.393

2@Shaggy Generally, no. If the two can be distinguished, undefined implies there's no valid output string. If the empty string (or any other string) is a valid output, claiming there is no valid output is incorrect. – Sara J – 2019-03-25T10:03:23.753

@Adám plus, if this were subsequence based then the duple target has a time to complete requirement, which would affect the submissions substantially. – Jonathan Allan – 2019-03-25T12:00:32.093

Related – Peter Taylor – 2019-03-25T12:24:44.033

Answers

8

Brachylog (v2), 3 9 bytes

{sᵛ}ᶠlᵒtw

Try it online!

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

Explanation

{sᵛ}ᶠlᵒtw
 s         Find a substring
  ᵛ          of every element {of the input}; the same one for each
{  }ᶠ      Convert generator to list
     lᵒt   Take list element with maximum length
        w  Output it

Apparently, the tiebreak order on s is not what it is in nearly everything else in Brachylog, so we need to manually override it to produce the longest output. (That's a bit frustrating: four extra characters for the override, plus two grouping characters because Brachylog doesn't parse two metapredicates in a row.)

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.

ais523

Posted 2019-03-25T00:35:03.313

Reputation: 11

1I tried this with the input ["many inpuabts", "any inabputs", "ny iabii", "yanabny"]. I expected the results ab and ny. But only got the result a. Am I doing something wrong ? – t-clausen.dk – 2019-03-25T11:54:08.340

1Ugh, seems I remembered the tiebreak order for s wrong, and overriding the tiebreak order is rather expensive byte-wise. Doing that now anyway, though, because it's important for the answer to be correct. Somehow none of the test cases I tried noticed the difference. – ais523 – 2019-03-25T12:21:24.497

1@ais523 The order s produces substrings is by first giving all prefixes of the input (longest first), then dropping the first one and repeat – Kroppeb – 2019-03-25T17:05:33.247

8

Python 2, 82 bytes

f=lambda h,*t:h and max(h*all(h in s for s in t),f(h[1:],*t),f(h[:-1],*t),key=len)

Try it online!

Takes input splatted. Will time out for inputs where the first string is long.

The idea is to take substrings of the first strings h to find the longest one that appears in all the remaining strings t. To do so, we recursively branch on removing the first or last character of h.


Python 2, 94 bytes

lambda l:max(set.intersection(*map(g,l)),key=len)
g=lambda s:s and{s}|g(s[1:])|g(s[:-1])or{''}

Try it online!

A more direct method. The auxiliary function g generates the set all substrings of s, and the main function takes the longest one in their intersection.

xnor

Posted 2019-03-25T00:35:03.313

Reputation: 115 687

5

Jelly, 12 6 bytes

Ẇ€f/ṫ0

Try it online!

Thanks to @JonathanAllan for saving 6 bytes!

Nick Kennedy

Posted 2019-03-25T00:35:03.313

Reputation: 11 829

I believe Ẇ€œ&/Ṫḟ0 would do the job and save four bytes since the sub-strings are already ordered by length, hence the filtered result will be; then all that remains is that when there are no matches tail produces a zero, and since we are guaranteed lists of characters we can simply filter those out. – Jonathan Allan – 2019-03-25T11:54:52.347

also I think œ&/ may be replaced by f/ here saving another – Jonathan Allan – 2019-03-25T12:03:02.170

I submitted a pull request to (hopefully) make the result of reducing an empty list be an empty list (rather than raising a TypeError). If that gets merged in I believe that this answer could then become a six-byter with Ẇ€f/ṛ/.

– Jonathan Allan – 2019-03-25T15:59:15.547

@JonathanAllan sounds good. Thanks for the other tips - hope you were happy for me to incorporate them. – Nick Kennedy – 2019-03-25T16:25:01.543

Yes, my reason for those comments was to allow you to incorporate the ideas into your post. – Jonathan Allan – 2019-03-25T16:39:44.910

...in fact even without the pull request being merged in we can have a full-program implementation in 6 bytes with Ẇ€f/ṫ0 (tailing from index zero, the rightmost, onward) and implicitly printing the result (as a monadic Link it yields an empty list or a list containing a single list of characters, but the implicit printing just prints the characters that are present). – Jonathan Allan – 2019-03-25T18:01:41.987

5

Ruby 2.6, 76 59 54 bytes

->a{*w=a;w.find{|r|w<<r.chop<<r[1..];a.all?{|s|s[r]}}}

Try it online! - Ruby 2.5 version (56 bytes)

How?

Create a list of potential matches, initially set to the original array. Iterate on the list, and if a string does not match, add 2 new strings to the tail of the list, chopping off the first or the last character. At the end a match (eventually an empty string) will be found.

Thanks Kirill L for -2 bytes and histocrat for another -2

G B

Posted 2019-03-25T00:35:03.313

Reputation: 11 099

4

Zsh, 126 ... 96 bytes

-3 bytes from arithmetic for, -6 bytes from implicit "$@" (thanks roblogic), -5 bytes from removing unneeded { }, -1 byte from short form of for, -1 byte by using repeat, -1 byte by concatenating for s ($b) with its body, -13 bytes by changing the repeat loop out for some eval jank.

for l
eval a=\( \$l\[{1..$#l},{1..$#l}\] \)&&b=(${${b-$a}:*a})
for s ($b)(($#x<$#s))&&x=$s
<<<$x

Try it online! Try it online! Try it online!

We read all possible substrings into the arraya, and then set b to the intersection of the arrays a and b. The construct ${b-$a} will only substitue $a on the first iteration: Unlike its sibling expansion ${b:-$a}, it will not substitute when b is set but empty.

for l;                              # implicit "$@"

# === OLD ===
{
    a= i=                           # empty a and i
    repeat $[$#l**2]                # compound double loop using div/mod
        a+=($l[++i/$#l+1,i%$#l+1])  # append to a all possible substrings of the given line
#               1+i/$#l             # 1,1,1...,  1,1,2,2,2,... ...,  n,n
#                       1+i%$#l     # 1,2,3...,n-1,n,1,2,3,... ...,n-1,n
#       a+=( $l[       ,     ] )    # append that substring to the array
# === NEW ===
    eval a=\( \
        \$l\[{1..$#l},{1..$#l}\] \  # The {bracket..expansions} are not escaped
    \) &&
# ===     ===
    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 s ($b)                          # for every common substring
    (( $#x < $#s )) && x=$s         # if the current word is longer, use it
<<<$x                               # print to stdout

GammaFunction

Posted 2019-03-25T00:35:03.313

Reputation: 2 838

How does this bit work? a+=( $l[1+i/$#l,1+i%$#l] ) – roblogic – 2019-03-26T12:44:39.530

1@roblogic I think I explained it better now, check the edit. The idea is to loop to n^2 and use / and % instead of using 2 nested for loops – GammaFunction – 2019-03-26T13:13:11.087

1you might be able to cut for l in "$@" to simply for l; - it's a bash trick – roblogic – 2019-03-26T16:41:54.077

i gotta say, zsh is so much more elegant than bash. There's nothing analogous to this nice array comparison AFAIK b=(${${b-$a}:*a})} – roblogic – 2019-03-27T01:48:05.080

1There's some neat things you can do with it, and it isn't all that popular. That translates into me adding a zsh answer to most questions I come across. :P If you want to learn zsh, I recommend man zshexpn and man zshparam especially. I always have them open when writing an answer. – GammaFunction – 2019-03-27T07:33:44.557

4

05AB1E, 14 9 8 bytes

€Œ.«ÃéθJ

-6 bytes thanks to @Adnan.

Try it online or verify all test cases.

Explanation:

€Œ       # Get the substring of each string in the (implicit) input-list
  .«     # Right-reduce this list of list of strings by:
    Ã    #  Only keep all the strings that are present in both list of strings
     é   # Sort by length
      θ  # And pop and push its last item
         # The substrings exclude empty items, so if after the reduce an empty list remains,
         # the last item will also be an empty list,
       J # which will become an empty string after a join
         # (after which the result is output implicitly)

Kevin Cruijssen

Posted 2019-03-25T00:35:03.313

Reputation: 67 575

1

I think €Œ.«Ãõªéθ should work for 9 bytes.

– Adnan – 2019-03-25T14:22:42.420

@Adnan Wait, we do have a reduce.. How did I miss that. :S I tried Å«Ã, but didn't realize I should have used .«Ã instead.. Thanks! – Kevin Cruijssen – 2019-03-25T14:25:03.670

1

Actually, I think €Œ.«ÃéθJ should work for 8.

– Adnan – 2019-03-25T19:58:06.200

4

R, 119 116 108 106 bytes

function(S,`?`=nchar,K=max(?S),s=Reduce(intersect,lapply(S,substring,0:K,rep(0:K,e=K+1))))s[which.max(?s)]

Try it online!

Find all substrings of each string, find the intersection of each list of substrings, then finally return (one of) the longest.

-3 bytes thanks to Kirill L.

-8 bytes using lapply instead of Map

-2 bytes thanks to Kirill L. again, removing braces

Giuseppe

Posted 2019-03-25T00:35:03.313

Reputation: 21 077

I don't have time to check, but if I'm not mistaken, 2 occurrences of nchar are enough to save something by declaring nchar as an unary operator. – Kirill L. – 2019-03-25T16:19:47.993

@KirillL. yes, it is shorter by 2 bytes. Thanks! Aliasing list similarly gives us -3 bytes. – Giuseppe – 2019-03-25T16:27:47.987

You can also drop braces for another -2

– Kirill L. – 2019-03-25T22:06:12.573

@KirillL. thanks! I'm a bit worried I'm going to start doing that with production code... – Giuseppe – 2019-03-25T22:15:07.033

that's the problem with golfing a language you use every day – MickyT – 2019-03-25T22:52:00.013

3

Perl 6, 62 60 bytes

{~sort(-*.comb,keys [∩] .map(*.comb[^*X.. ^*]>>.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 use sort instead. As pointed out in the comments, max can take an argument, however it ends up longer since I have to take into account max returning negative infinity when there are common substrings (Try it online!).

Jo King

Posted 2019-03-25T00:35:03.313

Reputation: 38 234

3max can take such a function (arity 1 though), either by position when called in OO mode, or a named :by argument in procedural mode. – Ven – 2019-03-25T09:31:40.837

3

Haskell, 80 bytes

import Data.List
f(x:r)=last$sortOn(0<$)[s|s<-inits=<<tails x,all(isInfixOf s)r]

Try it online!

Get all suffixes (tails) of the first word x in the list and take all prefixes (inits) of those suffixes to get all substrings s of x. Keep each s that isInfixOf all strings in the remaining list r. Sort those substrings by length (using the (0<$) trick) and return the last.

Laikoni

Posted 2019-03-25T00:35:03.313

Reputation: 23 676

3

TSQL query, 154 bytes

USE master
DECLARE @ table(a varchar(999)collate Latin1_General_CS_AI,i int identity)
INSERT @ values('string'),('stRIng');

SELECT top 1x FROM(SELECT
distinct substring(a,f.number,g.number)x,i
FROM spt_values f,spt_values g,@ WHERE'L'=g.type)D
GROUP BY x ORDER BY-sum(i),-len(x)

Try it online

Made case sensitive by declaring the column 'a' with collation containing CS (case sensitive).

Splitting all strings from 2540 starting positions(many identical) but the useful values range between 1 and 2070 and ending 0 to 22 characters after starting position, the end position could be longer by changing the type to 'P' instead of 'L', but would cripple performance.

These distinct strings within each rownumber are counted. The highest count will always be equal to the number of rows in the table variable '@'. Reversing the order on the same count will leave the substring with most matches on top of the results followed by reversed length of the substring will leave longest match with most matches on top. The query only select the top 1 row.

In order to get all answers, change the first part of the query to

SELECT top 1with ties x FROM

t-clausen.dk

Posted 2019-03-25T00:35:03.313

Reputation: 2 874

3

C# (Visual C# Interactive Compiler), 320 257 bytes

l=>(string.Join(",",l.Select(s=>new int[s.Length*s.Length*2].Select((i,j)=>string.Concat(s.Skip(j/-~s.Length).Take(j%-~s.Length))))
.Aggregate((a,b)=>a.Intersect(b)).GroupBy(x=>x.Length).OrderBy(x =>x.Key).LastOrDefault()?.Select(y=>y)??new List<string>()));

Try it online!

Props to @Expired Data and @dana

Innat3

Posted 2019-03-25T00:35:03.313

Reputation: 791

You can just return the string from a function.

So in Interactive compiler it might look something like this: 294 bytes

– Expired Data – 2019-03-25T16:45:24.157

1

Here's the solution golfed down some bytes 215 bytes

– Expired Data – 2019-03-25T17:00:24.470

@ExpiredData ah perfect this is the example I needed :) – Innat3 – 2019-03-26T08:22:22.833

186 – dana – 2019-03-27T03:22:09.397

184 – Embodiment of Ignorance – 2019-03-27T19:12:02.700

3

Retina 0.8.2, 48 bytes

M&!`(?<=^.*)(.+)(?=(.*\n.*\1)*.*$)
O#$^`
$.&
1G`

Try it online! Explanation:

M&!`(?<=^.*)(.+)(?=(.*\n.*\1)*.*$)

For each suffix of the first string, find the longest prefix that's also a substring of all of the other strings. List all of those suffix prefixes (i.e. substrings). If there are no matching substrings, we just end up with the empty string, which is what we want anyway.

O#$^`
$.&

Sort the substrings in reverse order of length.

1G`

Keep only the first, i.e. the longest substring.

Neil

Posted 2019-03-25T00:35:03.313

Reputation: 95 035

Let n is number of argument strings. Then (?=(.*\n.*\1)*.*$) should be (?=(.*\n.*\1){n-1}.*$), isn't it? Test case: ["very", "different", "much"] -> [""] – mazzy – 2019-03-27T09:11:22.833

1

@mazzy I don't see the problem: Try it online!

– Neil – 2019-03-27T09:41:07.457

it is not a problem. with {n} you could remove start and end patterns and keep (.+)(?=(.*\n.*\1){n} if Retina allows to write n shorter than (?<=^.*).*$ – mazzy – 2019-03-27T10:02:32.690

@mazzy I can't in Retina 0.8.2, and in Retina 1 I'd have to mess around with eval, which would probably be longer anyway. – Neil – 2019-03-27T10:35:43.330

2

Japt v2.0a0 -hF, 8 bytes

Îã f@eøX

Thanks to Shaggy for saving 3 bytes

Try it

Îã              //Generate all substrings of the first string
 f@             //Filter; keep the substrings that satisfy the following predicate:
   e            //    If all strings of the input...
    øX          //    Contain this substring, then keep it
-h              //Take last element
-F              //If last element is undefined, default to empty string

Embodiment of Ignorance

Posted 2019-03-25T00:35:03.313

Reputation: 7 014

You shouldn't need to sort by length at the end, saving 3 bytes. Also, -F defaults to the empty string. – Shaggy – 2019-03-25T11:49:54.237

2

Japt -h, 8 bytes

(I could knock off the last 3 bytes and use the -Fh flag instead but I'm not a fan of using -F)

mã rf iP

Try it or run all test cases

mã rf iP     :Implicit input of array
m            :Map
 ã           :  Substrings
   r         :Reduce by
    f        :  Filter, keeping only elements that appear in both arrays
      i      :Prepend
       P     :  An empty string
             :Implicit output of last element

Shaggy

Posted 2019-03-25T00:35:03.313

Reputation: 24 623

1

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!

Artemis still doesn't trust SE

Posted 2019-03-25T00:35:03.313

Reputation: 525

You may want to use single spaces as indentation instead of 4 that seems to shave more than 100 bytes. – Shieru Asakoto – 2019-03-25T01:21:17.013

Right, here's a fixed version of the 135 byte program

– Jo King – 2019-03-25T02:29:38.120

1

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.

Jo King

Posted 2019-03-25T00:35:03.313

Reputation: 38 234

1 byte shorter with set.intersection. – ovs – 2019-03-25T11:45:31.220

1

C# (Visual C# Interactive Compiler), 147 145 bytes

a=>{int i=0,j,m=0,k=a[0].Length;string s="",d=s;for(;i<k;i++)for(j=m;j++<k-i;)if(a.All(y=>y.Contains(s=a[0].Substring(i,j)))){m=j;d=s;}return d;}

Try it online!

Expired Data

Posted 2019-03-25T00:35:03.313

Reputation: 3 129

1

JavaScript (ES6),  98  92 bytes

a=>(g=b=s=>a.every(x=>~x.indexOf(s))?b=b[s.length]?b:s:g(s.slice(0,-1,g(s.slice(1)))))(a[0])

Try it online!

Arnauld

Posted 2019-03-25T00:35:03.313

Reputation: 111 334

1

Perl 5 (-aln0777F/\n/ -M5.01 -MList::util=max), 99 bytes

may be golfed more certainly

map/(.+)(?!.*\1)(?{$h{$&}++})(?!)/,@F;say for grep{y///c==max map y///c,@b}@b=grep@F==$h{$_},keys%h

TIO

Nahuel Fouilleul

Posted 2019-03-25T00:35:03.313

Reputation: 5 582

1

Bash, 295 .. 175 bytes

Not pretty but at least it works. Try it Online

-37 by general cleaning up ; -52 by plagiarising from the zsh answer ; -26 by replacing array with a loop ; -2 thanks to GammaFunction ; -3 removed i=0 from for loop

for l;{ d=${#l}
for((;i<d**2;i++)){ a="${l:i/d:1+i%d}" k=
for n;{ [[ $n =~ $a ]]&&((k++));}
((k-$#))||b+=("$a");};}
for e in "${b[@]}";do((${#e}>${#f}))&&f="$e";done
echo "$f"

Here's the original ungolfed script with comments

roblogic

Posted 2019-03-25T00:35:03.313

Reputation: 554

1Save 2 more: You can replace ((k==$#))&& with ((k-$#))||. This also lets you use k= instead of setting it to 0. – GammaFunction – 2019-03-27T07:24:31.850

1I think "Not pretty but at least it works" is the MO for bash scripts :) – joeytwiddle – 2019-03-27T09:39:27.647

1

Charcoal, 30 bytes

≔⊟θη≔⁰ζFLη«≔✂ηζ⊕ι¹ε¿⬤θ№κεPε≦⊕ζ

Try it online! Link is to verbose version of code. This algorithm is more efficient as well as shorter than generating all substrings. Explanation:

≔⊟θη

Pop the last string from the input list into a variable.

≔⁰ζ

Zero out the substring start index.

FLη«

Loop over all possible substring end indices. (Actually this loops from 0 excluding the length, so the value is adjusted later.)

≔✂ηζ⊕ι¹ε

Obtain the current substring.

¿⬤θ№κε

Check whether this substring is contained in all of the other input strings.

Pε

If it is then overprint any previously output substring.

≦⊕ζ

Otherwise try incrementing the substring start index.

Neil

Posted 2019-03-25T00:35:03.313

Reputation: 95 035

0

JavaScript (Node.js), 106 bytes

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!

a=>(                      // Main function
 F=(                      //  Helper function to run through all substrings in a[0]
  l,                      //   Length
  n,                      //   Start position
  w=a[0].substr(n,l)      //   The substring
 )=>
 l?                       //   If l > 0:
  n<0?                    //    If n < 0:
   F(--l,L-l)             //     Check another length
  :a.some(                //    If n >= 0: 
   y=>y.indexOf(w)<0      //     Check whether there is any string not containing the substring
                          //     (indexOf used because of presence of regex special characters)
  )?                      //     If so:
   F(l,n-1)               //      Check another substring
  :w                      //     If not, return this substring and terminate
                          //     (This function checks from the longest substring possible, so
                          //      it is safe to return right here)
 :""                      //   If l <= 0: Return empty string (no common substring)
)(
 L=a[0].length,           //  Starts from length = the whole length of a[0]
 0                        //  And start position = 0
)

Shieru Asakoto

Posted 2019-03-25T00:35:03.313

Reputation: 4 445

0

Java (JDK), 158 bytes

a->{for(int l=a.get(0).length(),i=l,j;i>=0;i--)for(j=0;j<l-i;){var s=a.get(0).substring(j++,j+i);if(a.stream().allMatch(x->x.contains(s)))return s;}return"";}

Try it online!

Credits

Olivier Grégoire

Posted 2019-03-25T00:35:03.313

Reputation: 10 647

1159 bytes (removed test cases to fit TIO link into a comment) – Sara J – 2019-04-03T02:03:25.120

0

Red, 266 174 bytes

func[b][l: length? s: b/1 n: 1 until[i: 1 loop n[t: copy/part at s i l r: on foreach u
next b[r: r and(none <> find/case u t)]if r[return t]i: i + 1]n: n + 1 1 > l: l - 1]""]

Try it online!

Changed the recursion to iteration and got rid of the sorting.

Galen Ivanov

Posted 2019-03-25T00:35:03.313

Reputation: 13 815

0

Perl 5, 87 bytes

my$r;"$&@_"=~/(.{@{[$r=~y,,,c]},}).*(\n.*\1.*){@{[@_-1]}}/ and$r=$1while$_[0]=~s,.,,;$r

Try it online!

Kjetil S.

Posted 2019-03-25T00:35:03.313

Reputation: 1 049

0

Gaia, 15 bytes

eḋ¦&⊢⟨:l¦:⌉=¦⟩∇

Try it online!

e		| eval as code
 ḋ¦		| find all non-empty substrings
   &⊢		| Reduce by set intersection
              ∇	| and return the first element where
      ⟨:l¦:⌉=¦⟩	| the length is equal to the max length

Giuseppe

Posted 2019-03-25T00:35:03.313

Reputation: 21 077

0

PowerShell, 165 163 87 bytes

-76 bytes thanks to Nail for the awesome regexp.

"$($args-join'
'|sls "(?<=^.*)(.+)(?=(.*\n.*\1)*.*$)"-a -ca|% m*|sort Le*|select -l 1)"

Try it online!

Less golfed:

$multilineArgs = $args-join"`n"
$matches = $multilineArgs|sls "(?<=^.*)(.+)(?=(.*\n.*\1)*.*$)" -AllMatches -CaseSensitive|% matches
$longestOrNull = $matches|sort Length|select -Last 1
"$longestOrNull"

mazzy

Posted 2019-03-25T00:35:03.313

Reputation: 4 832

0

JavaScript (Node.js), 90 bytes

f=(a,s=e=0,w='')=>a[0][e]?f(a,s+(r=a.some(x=>!x.includes(t),t=a[0].slice(s,++e))),r?w:t):w

Try it online! Test cases shamelessly stolen from @Arnauld. Port of my Charcoal answer.

Neil

Posted 2019-03-25T00:35:03.313

Reputation: 95 035

0

Perl 5, 86 bytes

$_=join"\x1",<>;$p=".*\x1.*\\1"x($.-1);say+(sort{length$b-length$a}/(.+)(?=$p)/msg)[0]

Try it online!

Xcali

Posted 2019-03-25T00:35:03.313

Reputation: 7 671

0

Pyth, 16 bytes

eolN.U@bZm{s./dQ

Try it online!

       m     Q    # Map Q (parsed input) over the following function (lambda d:
          ./d     # Partitions of d (all substrings)
         s        # Reduce on + (make one list)
        {         # deduplicate
    .U            # reduce the result on the following lambda, with starting value result[0]
      @bZ         # lambda b,Z: Intersection between b and Z
                  # Result so far: all common substrings in random order
 o                # sort the resulting sets by the following lambda function:
  lN              # lambda N: len(N)
e                 # last element of that list

ar4093

Posted 2019-03-25T00:35:03.313

Reputation: 531