Greatest common substringShortest Longest Common Subsequence CodeFinding “sub-palindromes” 2:...
What is this type of notehead called?
Find last 3 digits of this monster number
Why do IPv6 unique local addresses have to have a /48 prefix?
Transformation of random variables and joint distributions
Has Darkwing Duck ever met Scrooge McDuck?
Do the concepts of IP address and network interface not belong to the same layer?
Reply 'no position' while the job posting is still there
How will losing mobility of one hand affect my career as a programmer?
Should I stop contributing to retirement accounts?
Bob has never been a M before
Is there a conventional notation or name for the slip angle?
Visiting the UK as unmarried couple
How can "mimic phobia" be cured or prevented?
Freedom of speech and where it applies
Is it possible to have a strip of cold climate in the middle of a planet?
What is the grammatical term for “‑ed” words like these?
Is it better practice to read straight from sheet music rather than memorize it?
What (else) happened July 1st 1858 in London?
Longest common substring in linear time
Did arcade monitors have same pixel aspect ratio as TV sets?
Greatest common substring
How do I extrude a face to a single vertex
MAXDOP Settings for SQL Server 2014
Why does Async/Await work properly when the loop is inside the async function and not the other way around?
Greatest common substring
Shortest Longest Common Subsequence CodeFinding “sub-palindromes” 2: subsequences.Decompose a StringFind Patterns in StringsShortest Longest Common Subsequence CodeMaximal Substring ConstructionVisualize the greatest common divisorShortest Unique SubstringFind the original string, without the repetition without the repetition in the middleMinimal Fresh SubstringsStitch Together a Palindrome from Palindromic Substrings
$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.
- 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"]
code-golf string subsequence
$endgroup$
add a comment |
$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.
- 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"]
code-golf string subsequence
$endgroup$
$begingroup$
Possible duplicate
$endgroup$
– Adám
1 hour ago
$begingroup$
@Adám That question asks for the longest common subsequence, not substring.
$endgroup$
– Doorknob♦
1 hour ago
$begingroup$
Will the strings be only alphanumeric, or alphabetic, or only printable-ascii?
$endgroup$
– Embodiment of Ignorance
7 mins ago
add a comment |
$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.
- 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"]
code-golf string subsequence
$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.
- 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"]
code-golf string subsequence
code-golf string subsequence
asked 1 hour ago
Sara JSara J
1715
1715
$begingroup$
Possible duplicate
$endgroup$
– Adám
1 hour ago
$begingroup$
@Adám That question asks for the longest common subsequence, not substring.
$endgroup$
– Doorknob♦
1 hour ago
$begingroup$
Will the strings be only alphanumeric, or alphabetic, or only printable-ascii?
$endgroup$
– Embodiment of Ignorance
7 mins ago
add a comment |
$begingroup$
Possible duplicate
$endgroup$
– Adám
1 hour ago
$begingroup$
@Adám That question asks for the longest common subsequence, not substring.
$endgroup$
– Doorknob♦
1 hour ago
$begingroup$
Will the strings be only alphanumeric, or alphabetic, or only printable-ascii?
$endgroup$
– Embodiment of Ignorance
7 mins ago
$begingroup$
Possible duplicate
$endgroup$
– Adám
1 hour ago
$begingroup$
Possible duplicate
$endgroup$
– Adám
1 hour ago
$begingroup$
@Adám That question asks for the longest common subsequence, not substring.
$endgroup$
– Doorknob♦
1 hour ago
$begingroup$
@Adám That question asks for the longest common subsequence, not substring.
$endgroup$
– Doorknob♦
1 hour ago
$begingroup$
Will the strings be only alphanumeric, or alphabetic, or only printable-ascii?
$endgroup$
– Embodiment of Ignorance
7 mins ago
$begingroup$
Will the strings be only alphanumeric, or alphabetic, or only printable-ascii?
$endgroup$
– Embodiment of Ignorance
7 mins ago
add a comment |
4 Answers
4
active
oldest
votes
$begingroup$
Jelly, 12 bytes
Ẇ€œ&/LÐṀḢ¹L?
Try it online!
Last four bytes are there because of the requirement to only output one answer.
$endgroup$
add a comment |
$begingroup$
Not golfed it much yet but here's my Python 3 answer at 354 bytes:
EDIT: Now 238 bytes with one space indents and some one-line iterators:
EDIT: 193 bytes with max
function
EDIT: 189 bytes removed unnecessary list
def a(b):
a=();c=list(sum(list(list(d[f:e]for f in range(e))for e in range(len(d)+1)),[])for d in b)
for i in c[0]:
if all(i in j for j in c):
a+=(i,)
return max(a,key=len)
New contributor
$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
43 mins ago
$begingroup$
@ShieruAsakoto Oops yeah.
$endgroup$
– Artemis Fowl
36 mins ago
$begingroup$
135 bytes by simplifying thec
declaration and condensing thefor
loop. I would recommend adding the title and bytecount to the header, or using TIO's formatter to create the body of your post
$endgroup$
– Jo King
7 mins ago
$begingroup$
102 bytes by using set operators instead
$endgroup$
– Jo King
3 mins ago
add a comment |
$begingroup$
JavaScript (Node.js), 114 105 bytes
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.
y.indexOf
must be used in place of y.search
if the strings may contain special regex characters, at the cost of 1 more byte.
$endgroup$
add a comment |
$begingroup$
Brachylog, 3 bytes
sᵛw
Try it online!
Full program.
$endgroup$
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Jelly, 12 bytes
Ẇ€œ&/LÐṀḢ¹L?
Try it online!
Last four bytes are there because of the requirement to only output one answer.
$endgroup$
add a comment |
$begingroup$
Jelly, 12 bytes
Ẇ€œ&/LÐṀḢ¹L?
Try it online!
Last four bytes are there because of the requirement to only output one answer.
$endgroup$
add a comment |
$begingroup$
Jelly, 12 bytes
Ẇ€œ&/LÐṀḢ¹L?
Try it online!
Last four bytes are there because of the requirement to only output one answer.
$endgroup$
Jelly, 12 bytes
Ẇ€œ&/LÐṀḢ¹L?
Try it online!
Last four bytes are there because of the requirement to only output one answer.
answered 54 mins ago
Nick KennedyNick Kennedy
92147
92147
add a comment |
add a comment |
$begingroup$
Not golfed it much yet but here's my Python 3 answer at 354 bytes:
EDIT: Now 238 bytes with one space indents and some one-line iterators:
EDIT: 193 bytes with max
function
EDIT: 189 bytes removed unnecessary list
def a(b):
a=();c=list(sum(list(list(d[f:e]for f in range(e))for e in range(len(d)+1)),[])for d in b)
for i in c[0]:
if all(i in j for j in c):
a+=(i,)
return max(a,key=len)
New contributor
$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
43 mins ago
$begingroup$
@ShieruAsakoto Oops yeah.
$endgroup$
– Artemis Fowl
36 mins ago
$begingroup$
135 bytes by simplifying thec
declaration and condensing thefor
loop. I would recommend adding the title and bytecount to the header, or using TIO's formatter to create the body of your post
$endgroup$
– Jo King
7 mins ago
$begingroup$
102 bytes by using set operators instead
$endgroup$
– Jo King
3 mins ago
add a comment |
$begingroup$
Not golfed it much yet but here's my Python 3 answer at 354 bytes:
EDIT: Now 238 bytes with one space indents and some one-line iterators:
EDIT: 193 bytes with max
function
EDIT: 189 bytes removed unnecessary list
def a(b):
a=();c=list(sum(list(list(d[f:e]for f in range(e))for e in range(len(d)+1)),[])for d in b)
for i in c[0]:
if all(i in j for j in c):
a+=(i,)
return max(a,key=len)
New contributor
$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
43 mins ago
$begingroup$
@ShieruAsakoto Oops yeah.
$endgroup$
– Artemis Fowl
36 mins ago
$begingroup$
135 bytes by simplifying thec
declaration and condensing thefor
loop. I would recommend adding the title and bytecount to the header, or using TIO's formatter to create the body of your post
$endgroup$
– Jo King
7 mins ago
$begingroup$
102 bytes by using set operators instead
$endgroup$
– Jo King
3 mins ago
add a comment |
$begingroup$
Not golfed it much yet but here's my Python 3 answer at 354 bytes:
EDIT: Now 238 bytes with one space indents and some one-line iterators:
EDIT: 193 bytes with max
function
EDIT: 189 bytes removed unnecessary list
def a(b):
a=();c=list(sum(list(list(d[f:e]for f in range(e))for e in range(len(d)+1)),[])for d in b)
for i in c[0]:
if all(i in j for j in c):
a+=(i,)
return max(a,key=len)
New contributor
$endgroup$
Not golfed it much yet but here's my Python 3 answer at 354 bytes:
EDIT: Now 238 bytes with one space indents and some one-line iterators:
EDIT: 193 bytes with max
function
EDIT: 189 bytes removed unnecessary list
def a(b):
a=();c=list(sum(list(list(d[f:e]for f in range(e))for e in range(len(d)+1)),[])for d in b)
for i in c[0]:
if all(i in j for j in c):
a+=(i,)
return max(a,key=len)
New contributor
edited 11 mins ago
New contributor
answered 47 mins ago
Artemis FowlArtemis Fowl
1013
1013
New contributor
New contributor
$begingroup$
You may want to use single spaces as indentation instead of 4 that seems to shave more than 100 bytes.
$endgroup$
– Shieru Asakoto
43 mins ago
$begingroup$
@ShieruAsakoto Oops yeah.
$endgroup$
– Artemis Fowl
36 mins ago
$begingroup$
135 bytes by simplifying thec
declaration and condensing thefor
loop. I would recommend adding the title and bytecount to the header, or using TIO's formatter to create the body of your post
$endgroup$
– Jo King
7 mins ago
$begingroup$
102 bytes by using set operators instead
$endgroup$
– Jo King
3 mins ago
add a comment |
$begingroup$
You may want to use single spaces as indentation instead of 4 that seems to shave more than 100 bytes.
$endgroup$
– Shieru Asakoto
43 mins ago
$begingroup$
@ShieruAsakoto Oops yeah.
$endgroup$
– Artemis Fowl
36 mins ago
$begingroup$
135 bytes by simplifying thec
declaration and condensing thefor
loop. I would recommend adding the title and bytecount to the header, or using TIO's formatter to create the body of your post
$endgroup$
– Jo King
7 mins ago
$begingroup$
102 bytes by using set operators instead
$endgroup$
– Jo King
3 mins 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
43 mins 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
43 mins ago
$begingroup$
@ShieruAsakoto Oops yeah.
$endgroup$
– Artemis Fowl
36 mins ago
$begingroup$
@ShieruAsakoto Oops yeah.
$endgroup$
– Artemis Fowl
36 mins ago
$begingroup$
135 bytes by simplifying the
c
declaration and condensing the for
loop. I would recommend adding the title and bytecount to the header, or using TIO's formatter to create the body of your post$endgroup$
– Jo King
7 mins ago
$begingroup$
135 bytes by simplifying the
c
declaration and condensing the for
loop. I would recommend adding the title and bytecount to the header, or using TIO's formatter to create the body of your post$endgroup$
– Jo King
7 mins ago
$begingroup$
102 bytes by using set operators instead
$endgroup$
– Jo King
3 mins ago
$begingroup$
102 bytes by using set operators instead
$endgroup$
– Jo King
3 mins ago
add a comment |
$begingroup$
JavaScript (Node.js), 114 105 bytes
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.
y.indexOf
must be used in place of y.search
if the strings may contain special regex characters, at the cost of 1 more byte.
$endgroup$
add a comment |
$begingroup$
JavaScript (Node.js), 114 105 bytes
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.
y.indexOf
must be used in place of y.search
if the strings may contain special regex characters, at the cost of 1 more byte.
$endgroup$
add a comment |
$begingroup$
JavaScript (Node.js), 114 105 bytes
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.
y.indexOf
must be used in place of y.search
if the strings may contain special regex characters, at the cost of 1 more byte.
$endgroup$
JavaScript (Node.js), 114 105 bytes
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.
y.indexOf
must be used in place of y.search
if the strings may contain special regex characters, at the cost of 1 more byte.
edited 6 mins ago
answered 16 mins ago
Shieru AsakotoShieru Asakoto
2,750317
2,750317
add a comment |
add a comment |
$begingroup$
Brachylog, 3 bytes
sᵛw
Try it online!
Full program.
$endgroup$
add a comment |
$begingroup$
Brachylog, 3 bytes
sᵛw
Try it online!
Full program.
$endgroup$
add a comment |
$begingroup$
Brachylog, 3 bytes
sᵛw
Try it online!
Full program.
$endgroup$
Brachylog, 3 bytes
sᵛw
Try it online!
Full program.
answered 3 mins ago
community wiki
ais523
add a comment |
add a comment |
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).
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
$begingroup$
Possible duplicate
$endgroup$
– Adám
1 hour ago
$begingroup$
@Adám That question asks for the longest common subsequence, not substring.
$endgroup$
– Doorknob♦
1 hour ago
$begingroup$
Will the strings be only alphanumeric, or alphabetic, or only printable-ascii?
$endgroup$
– Embodiment of Ignorance
7 mins ago