5
\$\begingroup\$

Challenge

A simple challenge: given two or more strings, output all of the substrings that are in each string.

Rules

  • Input is always a comma separated list of strings
  • Strings should be outputted from longest to shortest length without duplicates
  • Strings should preserve whitespace

Examples

Input: aaa,aa
Output:
aa
a

Input: ababa,aba
Output:
aba
ab
ba
a
b

Input: Hello\, world!, Hello world!
Output:
 world!
world!
 world
 worl
world
orld!
Hello
orld
rld!
ello
 wor
Hell
worl
ell
rld
 wo
ld!
llo
wor
orl
Hel
d!
ll
 w
lo
rl
wo
or
He
el
ld
o
l
r
H
e
d
!
w
\$\endgroup\$

closed as unclear what you're asking by Wheat Wizard, xnor, Chris, Martin Ender Apr 18 '18 at 7:57

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    \$\begingroup\$ You do not have examples of more than two strings as input. \$\endgroup\$ – Chas Brown Apr 18 '18 at 2:43
  • 5
    \$\begingroup\$ In addition why is the input format so strict? The input format you require seems overly stringent. If I were to solve this the majority of my code would be an input parser. \$\endgroup\$ – Wheat Wizard Apr 18 '18 at 2:44
  • 2
    \$\begingroup\$ To pile on with What Wizard, \, is a pretty non-standard way of saying "don't split on this comma". Why not just take as input a list of strings? \$\endgroup\$ – Chas Brown Apr 18 '18 at 2:48
  • 2
    \$\begingroup\$ Also - welcome to PPCG! I didn't notice that this is your first post, and didn't mean to bite your head off! :) You might want to check out the sandbox where people often post their questions first so they can get helpful feedback including the usual sorts of expectations that people have for questions/challenges. \$\endgroup\$ – Chas Brown Apr 18 '18 at 2:59
  • 3
    \$\begingroup\$ Note that cumbersome I/O formats are discouraged. If you really want to enforce that, you should make it more explicit in the post (and the reason why you enforce it). \$\endgroup\$ – user202729 Apr 18 '18 at 7:52
2
\$\begingroup\$

05AB1E, 9 bytes

€Œ.»ÃÙéR»

Try it online!

Explanation

€Œ         # get the substrings of each
  .»Ã      # reduce by intersection
     Ù     # remove duplicates
      é    # sort by length
       R   # reverse
        »  # join on newline
\$\endgroup\$
1
\$\begingroup\$

Jelly, 6 bytes

Ẇ€f/QṚ

Try it online!

Ẇ€f/QṚ  Main Link
 €      For each string
Ẇ       Get all substrings
   /    Reduce this list of lists by
  f     Filter (intersection)
    Q   Remove duplicates
     Ṛ  Reverse
\$\endgroup\$
1
\$\begingroup\$

Pyth, 8

_{@Fm.:d

Explanation

        Q  # Implicit input list of 2 strings
    m.:d   # get lists of all substrings of each input string
  @F       # intersection
 {         # deduplicate
_          # reverse

Online test.

\$\endgroup\$
  • 1
    \$\begingroup\$ The comment should probably read "Implicit input list of strings", since it can be more than two strings. \$\endgroup\$ – Nit Apr 18 '18 at 7:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.