# Greatest common substring

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

• Possible duplicate – Adám Mar 25 at 0:51
• @Adám That question asks for the longest common subsequence, not substring. – Doorknob Mar 25 at 0:58
• Will the strings be only alphanumeric, or alphabetic, or only printable-ascii? – Embodiment of Ignorance Mar 25 at 1:57
• @EmbodimentofIgnorance All printable ASCII characters can appear in the input. – Sara J Mar 25 at 2:11
• @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 Mar 25 at 10:03

# 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.

# 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.

• I 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 Mar 25 at 11:54
• Ugh, 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 Mar 25 at 12:21
• @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 Mar 25 at 17:05

# Jelly, 12 6 bytes

Ẇ€f/ṫ0


Try it online!

Thanks to @JonathanAllan for saving 6 bytes!

• 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 Mar 25 at 11:54
• also I think œ&/ may be replaced by f/ here saving another – Jonathan Allan Mar 25 at 12:03
• 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 Mar 25 at 15:59
• @JonathanAllan sounds good. Thanks for the other tips - hope you were happy for me to incorporate them. – Nick Kennedy Mar 25 at 16:25
• Yes, my reason for those comments was to allow you to incorporate the ideas into your post. – Jonathan Allan Mar 25 at 16:39

# 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

# R, 119116108 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

• 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. Mar 25 at 16:19
• @KirillL. yes, it is shorter by 2 bytes. Thanks! Aliasing list similarly gives us -3 bytes. – Giuseppe Mar 25 at 16:27
• You can also drop braces for another -2 – Kirill L. Mar 25 at 22:06
• @KirillL. thanks! I'm a bit worried I'm going to start doing that with production code... – Giuseppe Mar 25 at 22:15
• that's the problem with golfing a language you use every day – MickyT Mar 25 at 22:52

# 05AB1E, 149 8 bytes

€Œ.«ÃéθJ


-6 bytes thanks to @Adnan.

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)

• I think €Œ.«Ãõªéθ should work for 9 bytes. – Adnan Mar 25 at 14:22
• @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 Mar 25 at 14:25
• Actually, I think €Œ.«ÃéθJ should work for 8. – Adnan Mar 25 at 19:58

# 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  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  • How does this bit work? a+=($l[1+i/$#l,1+i%$#l] ) – roblogic Mar 26 at 12:44
• @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 Mar 26 at 13:13
• you might be able to cut for l in "$@" to simply for l; - it's a bash trick – roblogic Mar 26 at 16:41 • 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 Mar 27 at 1:48
• There'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 Mar 27 at 7:33

# 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. # 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. • 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 Mar 27 at 9:11 • @mazzy I don't see the problem: Try it online! – Neil Mar 27 at 9:41 • 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 Mar 27 at 10:02
• @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 Mar 27 at 10:35

# 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

## 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

• You can just return the string from a function. So in Interactive compiler it might look something like this: 294 bytes – Expired Data Mar 25 at 16:45
• Here's the solution golfed down some bytes 215 bytes – Expired Data Mar 25 at 17:00
• @ExpiredData ah perfect this is the example I needed :) – Innat3 Mar 26 at 8:22
• 186 – dana Mar 27 at 3:22
• 184 – Embodiment of Ignorance Mar 27 at 19:12

# 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!).

• max 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 Mar 25 at 9:31

# 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

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

# 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

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


# 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!

• You may want to use single spaces as indentation instead of 4 that seems to shave more than 100 bytes. – Shieru Asakoto Mar 25 at 1:21
• @JoKing tio.run/… – Artemis Fowl Mar 25 at 2:20
• Right, here's a fixed version of the 135 byte program – Jo King Mar 25 at 2:29

# 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.

• 1 byte shorter with set.intersection. – ovs Mar 25 at 11:45

# 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!

# 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

# 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!

# Charcoal, 30 bytes

≔⊟θη≔⁰ζＦＬη«≔✂ηζ⊕ι¹ε¿⬤θ№κεＰε≦⊕ζ


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.

ＦＬη«


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.

Ｐε


If it is then overprint any previously output substring.

≦⊕ζ


Otherwise try incrementing the substring start index.

# 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 • Save 2 more: You can replace ((k==$#))&& with ((k-$#))||. This also lets you use k= instead of setting it to 0. – GammaFunction Mar 27 at 7:24 • I think "Not pretty but at least it works" is the MO for bash scripts :) – joeytwiddle Mar 27 at 9:39 # 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. # Perl 5, 87 bytes my$r;"$&@_"=~/(.{@{[$r=~y,,,c]},}).*(\n.*\1.*){@{[@_-1]}}/ and$r=$1while$_[0]=~s,.,,;$r


Try it online!

# 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
)


# 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

# PowerShell, 165163 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"  # 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. # 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 • 159 bytes (removed test cases to fit TIO link into a comment) – Sara J Apr 3 at 2:03 # Perl 5, 86 bytes $_=join"\x1",<>;$p=".*\x1.*\\1"x($.-1);say+(sort{length$b-length$a}/(.+)(?=\$p)/msg)[0]


Try it online!

# 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
`