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